Once again with a new dice-problem, and a malfunctioning script in python.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Smygis
    New Member
    • Jun 2007
    • 126

    #16
    Originally posted by BurnTard
    Roll four six-sided dice
    ignore the smallest
    add up the remaining, and note the result

    do over until you have got six values

    [code=python]import random

    for i in range(6):
    dices = []
    for i in range(4):
    dices.append(ra ndom.randint(1, 6))
    dices.sort()
    temp = 0
    for i in dices[1:]:
    temp += i
    print temp[/code]

    Comment

    • BurnTard
      New Member
      • May 2007
      • 52

      #17
      Originally posted by Smygis
      [code=python]import random

      for i in range(6):
      dices = []
      for i in range(4):
      dices.append(ra ndom.randint(1, 6))
      dices.sort()
      temp = 0
      for i in dices[1:]:
      temp += i
      print temp[/code]

      Oh, I like the look of that. Now could you explain the temp part? And the dices.sort()...

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #18
        While I like my class much better, this does the same thing without showing you too much about what's going on under the hood:[CODE=python]import random

        def role(intList):
        for i in range(len(intLi st)):
        intList[i] = random.randint( 1,6)

        def AddLowest(nItem s, intList):
        return sum(sorted(intL ist)[0:nItems])

        dice = [i for i in range(4)]
        role(dice)
        print dice
        print AddLowest(3, dice)


        nTests = 6
        for i in range(nTests):
        role(dice)
        print AddLowest(3, dice)
        [/CODE]

        Comment

        • Smygis
          New Member
          • Jun 2007
          • 126

          #19
          Originally posted by BurnTard
          Oh, I like the look of that. Now could you explain the temp part? And the dices.sort()...
          temp is simply a variable who contains the sum of the three largest values.

          dices.sort() sorts the list of dices with the smallest first. Witch then gets "sliced" away. and the rest is added to temp.

          Comment

          • BurnTard
            New Member
            • May 2007
            • 52

            #20
            Originally posted by bartonc
            While I like my class much better, this does the same thing without showing you too much about what's going on under the hood:[CODE=python]import random

            def role(intList):
            for i in range(len(intLi st)):
            intList[i] = random.randint( 1,6)

            def AddLowest(nItem s, intList):
            return sum(sorted(intL ist)[0:nItems])

            dice = [i for i in range(4)]
            role(dice)
            print dice
            print AddLowest(3, dice)


            nTests = 6
            for i in range(nTests):
            role(dice)
            print AddLowest(3, dice)
            [/CODE]
            I know I'm really really annoying now, but I still don't understand the AddLowest part. What is nItems supposed to be? And I haven't learned about "return" yet, but I suppose I can guess what it does.

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #21
              Originally posted by bartonc
              While I like my class much better, this does the same thing without showing you too much about what's going on under the hood:[CODE=python]import random

              def role(intList):
              for i in range(len(intLi st)):
              intList[i] = random.randint( 1,6)

              def AddLowest(nItem s, intList):
              return sum(sorted(intL ist)[0:nItems])

              dice = [i for i in range(4)]
              role(dice)
              print dice
              print AddLowest(3, dice)


              nTests = 6
              for i in range(nTests):
              role(dice)
              print AddLowest(3, dice)
              [/CODE]
              Oh.. Ignore the smallest:[CODE=python]import random

              def role(intList):
              for i in range(len(intLi st)):
              intList[i] = random.randint( 1,6)

              def AddLowest(nItem s, intList):
              return sum(sorted(intL ist)[0:nItems])

              def AddHighest(nIte ms, intList):
              return sum(sorted(intL ist)[-nItems:])

              dice = [i for i in range(4)]
              role(dice)
              print dice
              print AddHighest(3, dice)


              nTests = 6
              for i in range(nTests):
              role(dice)
              print AddHighest(3, dice)
              [/CODE]

              Comment

              • BurnTard
                New Member
                • May 2007
                • 52

                #22
                Originally posted by Smygis
                temp is simply a variable who contains the sum of the three largest values.

                dices.sort() sorts the list of dices with the smallest first. Witch then gets "sliced" away. and the rest is added to temp.
                Wow, that sounds both neat and easy! Thanks! Now I'll go listen to the other guy to see if he might actually manage to teach me something advanced =P

                Comment

                • Smygis
                  New Member
                  • Jun 2007
                  • 126

                  #23
                  Originally posted by Smygis
                  [code=python]
                  import random

                  for i in range(6):
                  dices = []
                  for i in range(4):
                  dices.append(ra ndom.randint(1, 6))
                  dices.sort()
                  temp = 0
                  for i in dices[1:]:
                  temp += i
                  print temp
                  [/code]
                  [code=python]
                  import random

                  for i in range(6):
                  dices = [random.randint( 1,6) for i in range(4)]
                  dices.sort()
                  print reduce(lambda x, y: x+y, dices[1:] )
                  [/code]

                  is a bit shorter tho
                  and:
                  [code=python]
                  for i in range(6): print reduce(lambda x, y: x+y, sorted([random.randint( 1,6) for i in range(4)])[1:] )
                  [/code]
                  is only one line. Totaly insane :D

                  Comment

                  • BurnTard
                    New Member
                    • May 2007
                    • 52

                    #24
                    Originally posted by Smygis
                    [code=python]
                    import random

                    for i in range(6):
                    dices = [random.randint( 1,6) for i in range(4)]
                    dices.sort()
                    print reduce(lambda x, y: x+y, dices[1:] )
                    [/code]

                    is a bit shorter tho
                    First, I don't know what reduce does, though it looks obvious, I can't define it in my head.

                    Second, it even says under my name at every post I make: Newbie. How am I supposed to know what lambda does? "Everybody shouts: GOOGLE IT!!!"
                    Well I did, and I did not understand it. Am I to stupid to make a script that rolls a couple of bloody dice and toys with them? Language minded...

                    Like some fellow said, the " # Comment/explanation for newbies here" is right handy!

                    Comment

                    • ilikepython
                      Recognized Expert Contributor
                      • Feb 2007
                      • 844

                      #25
                      Originally posted by Smygis
                      [code=python]
                      import random

                      for i in range(6):
                      dices = [random.randint( 1,6) for i in range(4)]
                      dices.sort()
                      print reduce(lambda x, y: x+y, dices[1:] )
                      [/code]

                      is a bit shorter tho
                      and:
                      [code=python]
                      for i in range(6): print reduce(lambda x, y: x+y, sorted([random.randint( 1,6) for i in range(4)])[1:] )
                      [/code]
                      is only one line. Totaly insane :D
                      Good job, that's creative. Keep posting!

                      Comment

                      • bartonc
                        Recognized Expert Expert
                        • Sep 2006
                        • 6478

                        #26
                        Originally posted by BurnTard
                        First, I don't know what reduce does, though it looks obvious, I can't define it in my head.

                        Second, it even says under my name at every post I make: Newbie. How am I supposed to know what lambda does? "Everybody shouts: GOOGLE IT!!!"
                        Well I did, and I did not understand it. Am I to stupid to make a script that rolls a couple of bloody dice and toys with them? Language minded...
                        Nope, you're not too stupid! My new friend, Smygis, has thrown some pretty advanced stuff at you. Use one of the more spread out versions and take it apart (note that there are many way to achieve a task in programming)

                        Comment

                        • BurnTard
                          New Member
                          • May 2007
                          • 52

                          #27
                          Originally posted by bartonc
                          Nope, you're not too stupid! My new friend, Smygis, has thrown some pretty advanced stuff at you. Use one of the more spread out versions and take it apart (note that there are many way to achieve a task in programming)
                          No offence, but according with my "level", your stuff was somewhat heavy too, even if I think I allmost got most of it now...

                          Comment

                          • Smygis
                            New Member
                            • Jun 2007
                            • 126

                            #28
                            Originally posted by BurnTard
                            First, I don't know what reduce does, though it looks obvious, I can't define it in my head.

                            Second, it even says under my name at every post I make: Newbie. How am I supposed to know what lambda does? "Everybody shouts: GOOGLE IT!!!"
                            Well I did, and I did not understand it. Am I to stupid to make a script that rolls a couple of bloody dice and toys with them? Language minded...
                            Dont frinkin google it, look it up in the Python Reference Manual's.
                            You dont realy need lambda, reduce and the others (map, filter).
                            They are the evils of Python ;) dont go there. I however, like them.

                            Start look at those when you feel ready for it.

                            Comment

                            • BurnTard
                              New Member
                              • May 2007
                              • 52

                              #29
                              Y'all can stop worrying now (yeah, as if) 'cause I solved it (Or rather, Smygis did)
                              I just stopped reading when he stopped making sense, and continued from there. In the end, it looked somewhat like this:

                              [code=python]

                              import random

                              for n in range(6)
                              dice=[random.randint( 1,6) for i in range(4)] # This was somewhat like when your maths-teacher does the worst exam-equation on the black-board, and you go, like. "Well of course, that was simple!" Though you know you'd have never figured it out by yourself.

                              dice.sort()
                              d2=dice[1:] # Probably a waste to not just go for editing dice rather than introducing d2, but I did not dare for fear of messing up...
                              print d2[0}+d2[1]+d2[2] # Could this be written: for n in d2: print n?

                              [/code]

                              And that's the hard part all done!

                              Comment

                              • Smygis
                                New Member
                                • Jun 2007
                                • 126

                                #30
                                Originally posted by BurnTard
                                Y'all can stop worrying now (yeah, as if) 'cause I solved it (Or rather, Smygis did)
                                I just stopped reading when he stopped making sense, and continued from there. In the end, it looked somewhat like this:

                                [code=python]

                                import random

                                for n in range(6)
                                dice=[random.randint( 1,6) for i in range(4)] # This was somewhat like when your maths-teacher does the worst exam-equation on the black-board, and you go, like. "Well of course, that was simple!" Though you know you'd have never figured it out by yourself.

                                dice.sort()
                                d2=dice[1:] # Probably a waste to not just go for editing dice rather than introducing d2, but I did not dare for fear of messing up...
                                print d2[0}+d2[1]+d2[2] # Could this be written: for n in d2: print n?

                                [/code]

                                And that's the hard part all done!
                                Other then d2[0} shuld be d2[0]
                                it looks mutch better then the first one.
                                and if you wrote for n in d2: print n it wold simply print the numbers not add them together.

                                Comment

                                Working...