By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

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

P: 52
Hello, I have a problem that's hugely annoying. The other day, me and a friend made a program for creating a Dungons and Dragons roleplaying character, and after about a day of work, it was working properly. Only for us to find that it did not fully agree with the rules of the game, and it was slightly flawed, so I decided to write a new one. Only problem? Most of the time, while the program should give an output of six numbers, it only gives me anything between two and five. So does one of the great geek spot the problem? Please tell me, it would be a great help! :)

So here's the temporary script:

Expand|Select|Wrap|Line Numbers
  1.  
  2. import random
  3.  
  4. for n in range(1,7):
  5.  
  6.  d1=random.randint(1,6)
  7.  d2=random.randint(1,6)
  8.  d3=random.randint(1,6)
  9.  d4=random.randint(1,6)
  10.  
  11.  
  12.  for n in range(1):
  13.   if d1<d2 and d1<d3 and d1<d4:
  14.      print d2+d3+d4
  15.      break
  16.  
  17.  
  18.   elif d2<d1 and d2<d3 and d2<d4:
  19.      print d1+d3+d4
  20.      break     
  21.  
  22.  
  23.   elif d3<d1 and d3<d2 and d3<d4:
  24.      print d1+d2+d4
  25.      break
  26.  
  27.  
  28.   elif d4<d1 and d4<d2 and d4<d3:
  29.      print d1+d2+d3
  30.      break
  31.  
  32.  
  33.   elif d1==d2:
  34.    if d1<d3:
  35.     if d1<d4:
  36.      print d2+d3+d4
  37.  
  38.  
  39.   elif d1==d3:
  40.    if d1<d2:
  41.     if d1<d4:
  42.      print d2+d3+d4
  43.  
  44.  
  45.   elif d1==d4:
  46.    if d1<d2:
  47.     if d1<d3:
  48.      print d2+d3+d4
  49.  
  50.  
  51.   elif d2==d1:
  52.    if d2<d3:
  53.     if d2<d4:
  54.      print d1+d3+d4
  55.  
  56.  
  57.   elif d2==d3:
  58.    if d2<d1:
  59.     if d2<d4:
  60.      print d1+d3+d4
  61.  
  62.  
  63.   elif d2==d4:
  64.    if d2<d1:
  65.     if d2<d3:
  66.      print d1+d3+d4
  67.  
  68.  
  69.   elif d3==d1:
  70.    if d3<d2:
  71.     if d3<d4:
  72.      print d1+d2+d4
  73.  
  74.  
  75.   elif d3==d2:
  76.    if d3<d1:
  77.     if d3<d4:
  78.      print d1+d2+d4
  79.  
  80.  
  81.   elif d3==d4:
  82.    if d3<d1:
  83.     if d3<d2:
  84.      print d1+d2+d4
  85.  
  86.  
  87.   elif d4==d1:
  88.    if d4<d2:
  89.     if d4<d3:
  90.      print d1+d2+d3
  91.  
  92.   elif d4==d2:
  93.    if d4<d1:
  94.     if d4<d3:
  95.      print d1+d2+d3
  96.  
  97.  
  98.   elif d4==d3:
  99.    if d4<d1:
  100.     if d4<d2:
  101.      print d1+d2+d3
  102.  
  103.  
  104.   elif d1==d2 and d1==d3:
  105.    if d1<d4:
  106.     print d2+d3+d4
  107.  
  108.  
  109.   elif d1==d2 and d1==d4:
  110.    if d1<d3:
  111.     print d2+d3+d
  112.  
  113.  
  114.   elif d1==d3 and d1==d4:
  115.    if d1<d2:
  116.     print d2+d3+d4
  117.  
  118.  
  119.   elif d2==d1 and d2==d3:
  120.    if d2<d4:
  121.     print d1+d3+d4
  122.  
  123.  
  124.   elif d2==d3 and d2==d4:
  125.    if d2<d1:
  126.     print d1+d3+d4
  127.  
  128.  
  129.   elif d2==d1 and d2==d4:
  130.    if d2<d3:
  131.     print d1+d3+d4
  132.  
  133.  
  134.   elif d3==d1 and d3==d2:
  135.    if d3<d4:
  136.     print d1+d2+d4
  137.  
  138.  
  139.   elif d3==d2 and d3==d4:
  140.    if d3<d1:
  141.     print d1+d2+d4
  142.  
  143.  
  144.   elif d3==d1 and d3==d4:
  145.    if d3<d2:
  146.     print d1+d2+d4
  147.  
  148.  
  149.   elif d4==d1 and d4==d2:
  150.    if d4<d3:
  151.     print d1+d2+d3
  152.  
  153.  
  154.   elif d4==d2 and d4==d3:
  155.    if d4<d1:
  156.     print d1+d2+d3
  157.  
  158.  
  159.   elif d4==d1 and d4==d3:
  160.    if d4<d2:
  161.     print d1+d2+d3
  162.  
  163.  
  164.   elif d1==d2 and d1==d3 and d1==d4 and d2==d1 and d2==d3 and d2==d4 and d3==d1 and d3==d2 and d3==d4 and d4==d1 and d4==d2 and d4==d3:
  165.    print d1+d2+d3
  166.  
  167.  
Jun 1 '07 #1
Share this Question
Share on Google+
31 Replies


Smygis
100+
P: 126
After removing several lines of code a realized that i culd not make sense of whats happening in youre code.

And i do not know the rules of that game so i gave up.

But there is a lot of strange things going on in that pace of code. And there is a lot of dead code in it aswell. No code after line 102 will ever be executet as far as i can see.

But please, Dont use space to indent. Use tab. Or atleast 3+ spaces, Then i culd atleast have used find/replace.
Jun 1 '07 #2

bartonc
Expert 5K+
P: 6,596
After removing several lines of code a realized that i culd not make sense of whats happening in youre code.

And i do not know the rules of that game so i gave up.

But there is a lot of strange things going on in that pace of code. And there is a lot of dead code in it aswell. No code after line 102 will ever be executet as far as i can see.

But please, Dont use space to indent. Use tab. Or atleast 3+ spaces, Then i culd atleast have used find/replace.
Thank you, Smygis, for your input. However, tabs are not necessary. 4 spaces per indent is what most Python editors use.
Jun 1 '07 #3

bvdet
Expert Mod 2.5K+
P: 2,851
I cannot tell what your are trying to do with the large number of 'if' statements. If we could find out what your rules are, we might be able to make a suggestion. This will print six numbers:
Expand|Select|Wrap|Line Numbers
  1. import random
  2. for n in range(6):
  3.  
  4.     dList = [random.randint(1,6) for _ in range(4)]
  5.  
  6.     s = random.sample([0,1,2,3], 3)
  7.     print dList[s[0]]+dList[s[1]]+dList[s[2]]
Jun 1 '07 #4

bartonc
Expert 5K+
P: 6,596
Hello, I have a problem that's hugely annoying. The other day, me and a friend made a program for creating a Dungons and Dragons roleplaying character, and after about a day of work, it was working properly. Only for us to find that it did not fully agree with the rules of the game, and it was slightly flawed, so I decided to write a new one. Only problem? Most of the time, while the program should give an output of six numbers, it only gives me anything between two and five. So does one of the great geek spot the problem? Please tell me, it would be a great help! :)
Our newest contributor, Smygis, is quite correct. The stucture of you code needs help. We need to help you understand the use of loops and find a more structured way of setting up the rules of the game. However, most Python experts may not know the rules of the game. One thing your code really lacks is comments explaining what it should do. I'll have a brief look, but it will help us help you if you could expain each section of code with comments. Thanks.
Jun 1 '07 #5

bartonc
Expert 5K+
P: 6,596
I think I see that this
Expand|Select|Wrap|Line Numbers
  1.     elif d3<d1 and d3<d2 and d3<d4:
  2.         print d1+d2+d4
  3.         break
is intended to print 3 numbers but is written to print the total of them instead.
Expand|Select|Wrap|Line Numbers
  1.     elif d3<d1 and d3<d2 and d3<d4:
  2.         print d1, d2, d4
  3.         break
This would print 3 numbers and exit the loop.
Jun 1 '07 #6

Smygis
100+
P: 126
Thank you, Smygis, for your input. However, tabs are not necessary. 4 spaces per indent is what most Python editors use.
I however prefer tab, But i use a tab with of 4 spaces. And some editors dont use the tab char and simply replace the tab with (4) spaces. What i dont like is one space as indent, It gets hard to read the code in my opinion.
Jun 1 '07 #7

bartonc
Expert 5K+
P: 6,596
I however prefer tab, But i use a tab with of 4 spaces. And some editors dont use the tab char and simply replace the tab with (4) spaces. What i dont like is one space as indent, It gets hart to read the code in my opinion.
I agree 100%.

Thank you for joining us here at TheScripts.
Jun 1 '07 #8

bartonc
Expert 5K+
P: 6,596
Here it is, re-indented. Just one command in Boa Constructor!
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. for n in range(1,7):
  4.  
  5.     d1=random.randint(1,6)
  6.     d2=random.randint(1,6)
  7.     d3=random.randint(1,6)
  8.     d4=random.randint(1,6)
  9.  
  10.  
  11.     for n in range(1):
  12.         if d1<d2 and d1<d3 and d1<d4:
  13.             print d2+d3+d4
  14.             break
  15.  
  16.  
  17.         elif d2<d1 and d2<d3 and d2<d4:
  18.             print d1+d3+d4
  19.             break
  20.  
  21.  
  22.         elif d3<d1 and d3<d2 and d3<d4:
  23.             print d1+d2+d4
  24.             break
  25.  
  26.  
  27.         elif d4<d1 and d4<d2 and d4<d3:
  28.             print d1+d2+d3
  29.             break
  30.  
  31.  
  32.         elif d1==d2:
  33.             if d1<d3:
  34.                 if d1<d4:
  35.                     print d2+d3+d4
  36.  
  37.  
  38.         elif d1==d3:
  39.             if d1<d2:
  40.                 if d1<d4:
  41.                     print d2+d3+d4
  42.  
  43.  
  44.         elif d1==d4:
  45.             if d1<d2:
  46.                 if d1<d3:
  47.                     print d2+d3+d4
  48.  
  49.  
  50.         elif d2==d1:
  51.             if d2<d3:
  52.                 if d2<d4:
  53.                     print d1+d3+d4
  54.  
  55.  
  56.         elif d2==d3:
  57.             if d2<d1:
  58.                 if d2<d4:
  59.                     print d1+d3+d4
  60.  
  61.  
  62.         elif d2==d4:
  63.             if d2<d1:
  64.                 if d2<d3:
  65.                     print d1+d3+d4
  66.  
  67.  
  68.         elif d3==d1:
  69.             if d3<d2:
  70.                 if d3<d4:
  71.                     print d1+d2+d4
  72.  
  73.  
  74.         elif d3==d2:
  75.             if d3<d1:
  76.                 if d3<d4:
  77.                     print d1+d2+d4
  78.  
  79.  
  80.         elif d3==d4:
  81.             if d3<d1:
  82.                 if d3<d2:
  83.                     print d1+d2+d4
  84.  
  85.  
  86.         elif d4==d1:
  87.             if d4<d2:
  88.                 if d4<d3:
  89.                     print d1+d2+d3
  90.  
  91.         elif d4==d2:
  92.             if d4<d1:
  93.                 if d4<d3:
  94.                     print d1+d2+d3
  95.  
  96.  
  97.         elif d4==d3:
  98.             if d4<d1:
  99.                 if d4<d2:
  100.                     print d1+d2+d3
  101.  
  102.  
  103.         elif d1==d2 and d1==d3:
  104.             if d1<d4:
  105.                 print d2+d3+d4
  106.  
  107.  
  108.         elif d1==d2 and d1==d4:
  109.             if d1<d3:
  110.                 print d2+d3+d
  111.  
  112.  
  113.         elif d1==d3 and d1==d4:
  114.             if d1<d2:
  115.                 print d2+d3+d4
  116.  
  117.  
  118.         elif d2==d1 and d2==d3:
  119.             if d2<d4:
  120.                 print d1+d3+d4
  121.  
  122.  
  123.         elif d2==d3 and d2==d4:
  124.             if d2<d1:
  125.                 print d1+d3+d4
  126.  
  127.  
  128.         elif d2==d1 and d2==d4:
  129.             if d2<d3:
  130.                 print d1+d3+d4
  131.  
  132.  
  133.         elif d3==d1 and d3==d2:
  134.             if d3<d4:
  135.                 print d1+d2+d4
  136.  
  137.  
  138.         elif d3==d2 and d3==d4:
  139.             if d3<d1:
  140.                 print d1+d2+d4
  141.  
  142.  
  143.         elif d3==d1 and d3==d4:
  144.             if d3<d2:
  145.                 print d1+d2+d4
  146.  
  147.  
  148.         elif d4==d1 and d4==d2:
  149.             if d4<d3:
  150.                 print d1+d2+d3
  151.  
  152.  
  153.         elif d4==d2 and d4==d3:
  154.             if d4<d1:
  155.                 print d1+d2+d3
  156.  
  157.  
  158.         elif d4==d1 and d4==d3:
  159.             if d4<d2:
  160.                 print d1+d2+d3
  161.  
  162.  
  163.         elif d1==d2 and d1==d3 and d1==d4 and d2==d1 and d2==d3 and d2==d4 and d3==d1 and d3==d2 and d3==d4 and d4==d1 and d4==d2 and d4==d3:
  164.             print d1+d2+d3
  165.  
Jun 1 '07 #9

bartonc
Expert 5K+
P: 6,596
Here's something with a bit better stucture:
Expand|Select|Wrap|Line Numbers
  1. # import needed modules
  2. import random
  3.  
  4. # define classes
  5. class Die(object):
  6.     """A class representing a single six-sided die.
  7.        A die of this class has an optional name.
  8.        __init__() calls self.role() so creation produces a roled die."""
  9.  
  10.     def __init__(self, name=""):
  11.         """Assign an optional name and role self."""
  12.         self.name = name
  13.         self.role()
  14.  
  15.     def __cmp__(self, other):
  16.         # overload the compare operation for ==, !=, <, >, etc #
  17.         return cmp(self.value, other.value)
  18.  
  19.     def __repr__(self):
  20.         """Identify self and report value when printed."""
  21.         return "My name is %s. My value is %d" %(repr(self.name), self.value)
  22.  
  23.  
  24.     def role(self):
  25.         """Assign a random value between 1 and 6 to the value of this instance."""
  26.         self.value = random.randint(1,6)
  27.  
  28.     def get(self):
  29.         """Just return the value of this instance."""
  30.         return self.value
  31.  
  32. ### define utility functions ###
  33. def RoleDice(dice):
  34.     """Given a list of die to role, role them.
  35.        Since lists are mutable, don't actually return the list."""
  36.     for die in dice:
  37.         die.role()
  38.  
  39. def PrintDice(dice):
  40.     """Call each die's __repr__() method with print."""
  41.     for die in dice:
  42.         print die
  43.  
  44.  
  45. ### Define rules ###
  46. def AllEqual(dice):
  47.     """True or False: Are all the dice of equal value?"""
  48.     for i in range(len(dice) - 1):
  49.         if dice[i] != dice[i + 1]:
  50.             # if any one is NOT EQUAL to another, the answer is false!
  51.             return False
  52.     # if it makes it this far, they are all equal
  53.     return True
  54.  
  55.  
  56. ### Create objects to work with ###
  57. # a list of dice names
  58. diceNames = ['First', 'Second', 'Third', 'Forth']
  59.  
  60. # a list of named dice
  61. dice = [Die(name) for name in diceNames]
  62.  
  63. # test compare #
  64. dice[0].value = 4
  65. dice[1].value = 4
  66. print dice[0] == dice[1]
  67.  
  68. # test object creation
  69. PrintDice(dice)
  70. print
  71.  
  72. # test utility functions and rules
  73. for i in range(5):   # run 5 tests
  74.     RoleDice(dice)
  75.     PrintDice(dice)
  76.     print "All dice are %s equal" %["not", ""][AllEqual(dice)]
  77.     print
with a class and doc strings thrown in for free!
Jun 1 '07 #10

P: 52
I think I see that this
Expand|Select|Wrap|Line Numbers
  1.     elif d3<d1 and d3<d2 and d3<d4:
  2.         print d1+d2+d4
  3.         break
is intended to print 3 numbers but is written to print the total of them instead.
Expand|Select|Wrap|Line Numbers
  1.     elif d3<d1 and d3<d2 and d3<d4:
  2.         print d1, d2, d4
  3.         break
This would print 3 numbers and exit the loop.

No, it is meant to print the sum. I know the code is rather messy. It started out pretty clean, but as my aggression built, my neatness decreased, and whenever it returned the wrong answer, I felt an urge to add more code. With all the "dead code" done, it gives an output of six numbers about half of the time. I'll see what I can do about comments and neatness though.
Jun 1 '07 #11

P: 52
Here's something with a bit better stucture:
Expand|Select|Wrap|Line Numbers
  1. # import needed modules
  2. import random
  3.  
  4. # define classes
  5. class Die(object):
  6.     """A class representing a single six-sided die.
  7.        A die of this class has an optional name.
  8.        __init__() calls self.role() so creation produces a roled die."""
  9.  
  10.     def __init__(self, name=""):
  11.         """Assign an optional name and role self."""
  12.         self.name = name
  13.         self.role()
  14.  
  15.     def __cmp__(self, other):
  16.         # overload the compare operation for ==, !=, <, >, etc #
  17.         return cmp(self.value, other.value)
  18.  
  19.     def __repr__(self):
  20.         """Identify self and report value when printed."""
  21.         return "My name is %s. My value is %d" %(repr(self.name), self.value)
  22.  
  23.  
  24.     def role(self):
  25.         """Assign a random value between 1 and 6 to the value of this instance."""
  26.         self.value = random.randint(1,6)
  27.  
  28.     def get(self):
  29.         """Just return the value of this instance."""
  30.         return self.value
  31.  
  32. ### define utility functions ###
  33. def RoleDice(dice):
  34.     """Given a list of die to role, role them.
  35.        Since lists are mutable, don't actually return the list."""
  36.     for die in dice:
  37.         die.role()
  38.  
  39. def PrintDice(dice):
  40.     """Call each die's __repr__() method with print."""
  41.     for die in dice:
  42.         print die
  43.  
  44.  
  45. ### Define rules ###
  46. def AllEqual(dice):
  47.     """True or False: Are all the dice of equal value?"""
  48.     for i in range(len(dice) - 1):
  49.         if dice[i] != dice[i + 1]:
  50.             # if any one is NOT EQUAL to another, the answer is false!
  51.             return False
  52.     # if it makes it this far, they are all equal
  53.     return True
  54.  
  55.  
  56. ### Create objects to work with ###
  57. # a list of dice names
  58. diceNames = ['First', 'Second', 'Third', 'Forth']
  59.  
  60. # a list of named dice
  61. dice = [Die(name) for name in diceNames]
  62.  
  63. # test compare #
  64. dice[0].value = 4
  65. dice[1].value = 4
  66. print dice[0] == dice[1]
  67.  
  68. # test object creation
  69. PrintDice(dice)
  70. print
  71.  
  72. # test utility functions and rules
  73. for i in range(5):   # run 5 tests
  74.     RoleDice(dice)
  75.     PrintDice(dice)
  76.     print "All dice are %s equal" %["not", ""][AllEqual(dice)]
  77.     print
with a class and doc strings thrown in for free!

Woah, that is way to complicated for me! I only got into Python some two weeks ago, if that long, and I have no idea what a class is, and definitions are way above me! Please, try to keep it simple!
Jun 1 '07 #12

bartonc
Expert 5K+
P: 6,596
No, it is meant to print the sum. I know the code is rather messy. It started out pretty clean, but as my aggression built, my neatness decreased, and whenever it returned the wrong answer, I felt an urge to add more code. With all the "dead code" done, it gives an output of six numbers about half of the time. I'll see what I can do about comments and neatness though.
Here's how a list of objects could help you solve the first part:
Expand|Select|Wrap|Line Numbers
  1.  
  2. def AddLowest(nItems, dice):
  3.     tmpList = sorted(dice)
  4.     valueList = [die.get() for die in tmpList[0:nItems]]
  5.     return sum(valueList), tmpList[0:nItems]
  6.  
  7. ### Create objects to work with ###
  8. # a list of dice names
  9. diceNames = ['First', 'Second', 'Third', 'Forth', 'Fifth', 'Sixth', 'Seventh']
  10.  
  11. # a list of named dice
  12. dice = [Die(name) for name in diceNames]
  13.  
  14. testResult = AddLowest(3, dice)
  15.  
  16. print "The sum of the lowest 3 dice is %d" %testResult[0]
  17. print "The lowest 3 dice are:"
  18. for die in testResult[1]:
  19.     print die
  20. print
with lots of test code thrown in for free!
Jun 1 '07 #13

P: 52
Okay, so I'm obviously not allowed to edit the first post... Well, I realize that I failed spectacularly in actually telling you guys what the script should do... Here is what the recipe is supposed to mean, if you were to do it in real life:

Roll four six-sided dice
ignore the smallest
add up the remaining, and note the result

do over until you have got six values

now this seemed rather simple to me at first, but I allways missed something, and when an event occurs that the script does not anticipate, apparently, it just breaks the loop and starts at the wee beginning. The script as it was when I first thought I was finished looked somewhat like this:

Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3.  
  4. d1="" # Most of the time I have to add these at the top of the script as I go, because otherwise the program gives me the outputs like "d1 not defined", because it didn't bother to roll the dice before checking the program for errors or some such nonsense...
  5. d2=""
  6. d3=""
  7. d4=""
  8.  
  9.  
  10.  
  11. for n in range(1,7): # So it does it six times of course
  12.  
  13.     d1=random.randint(1,6) # Rolls a six sided die
  14.     d2=random.randint(1,6) # Rolls a second six-sided die
  15.     d3=random.randint(1,6)
  16.     d4=random.randint(1,6)
  17.  
  18.     for n in range(1): # Just to create a loop which can be broken
  19.  
  20.         if d1<d2 and d1<d3 and d1<d4: # As in "if d1 is smallest"
  21.             print d2+d3+d4 # Sums them up and prints them
  22.             break # If I'm not wrong, this breaks the "for n in range(1)" loop, and goes to the second part of the "for n in range(1,7)" loop.
  23.  
  24.         elif d2<d1 and d2<d3 and d2<d4: # As in if the second die is smallest
  25.             print d1+d3+d4
  26.             break
  27.  
  28.         elif d3<d1 and d3<d2 and d3<d4:
  29.             print d1+d2+d4
  30.             break
  31.  
  32.         elif d4<d1 and d4<d2 and d4<d3:
  33.             print d1+d2+d3 
  34.             break
  35.  
  36.         elif d1==d2==d3==d4: # This is the short version, as you might have seen from the previous one. I tried this one first, but I don't think it does what I want it to...
  37.             print d1+d2+d3 # If they're all alike, it matters not which one I ignore
  38.             # And since this is the end, I should not have to break a loop, when it only repeats once anyways. Oh well....
  39.             break
  40.  
  41.  
So there it is, all properly indented and commented. My eyes fail to recognize the fault, but mayhap one of the hawks among you can?
Jun 1 '07 #14

P: 52
Here's how a list of objects could help you solve the first part:
Expand|Select|Wrap|Line Numbers
  1.  
  2. def AddLowest(nItems, dice):
  3.     tmpList = sorted(dice)
  4.     valueList = [die.get() for die in tmpList[0:nItems]]
  5.     return sum(valueList), tmpList[0:nItems]
  6.  
  7. ### Create objects to work with ###
  8. # a list of dice names
  9. diceNames = ['First', 'Second', 'Third', 'Forth', 'Fifth', 'Sixth', 'Seventh']
  10.  
  11. # a list of named dice
  12. dice = [Die(name) for name in diceNames]
  13.  
  14. testResult = AddLowest(3, dice)
  15.  
  16. print "The sum of the lowest 3 dice is %d" %testResult[0]
  17. print "The lowest 3 dice are:"
  18. for die in testResult[1]:
  19.     print die
  20. print
with lots of test code thrown in for free!

Ur... So... I didn't get that. I fell out, like, when you defined AddLowest
Jun 1 '07 #15

Smygis
100+
P: 126
Roll four six-sided dice
ignore the smallest
add up the remaining, and note the result

do over until you have got six values

Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. for i in range(6):
  4.     dices = []
  5.     for i in range(4):
  6.         dices.append(random.randint(1,6))
  7.     dices.sort()
  8.     temp = 0
  9.     for i in dices[1:]:
  10.         temp += i
  11.     print temp
Jun 1 '07 #16

P: 52
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. for i in range(6):
  4.     dices = []
  5.     for i in range(4):
  6.         dices.append(random.randint(1,6))
  7.     dices.sort()
  8.     temp = 0
  9.     for i in dices[1:]:
  10.         temp += i
  11.     print temp

Oh, I like the look of that. Now could you explain the temp part? And the dices.sort()...
Jun 1 '07 #17

bartonc
Expert 5K+
P: 6,596
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:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def role(intList):
  4.     for i in range(len(intList)):
  5.         intList[i] = random.randint(1,6)
  6.  
  7. def AddLowest(nItems, intList):
  8.     return sum(sorted(intList)[0:nItems])
  9.  
  10. dice = [i for i in range(4)]
  11. role(dice)
  12. print dice
  13. print AddLowest(3, dice)
  14.  
  15.  
  16. nTests = 6
  17. for i in range(nTests):
  18.     role(dice)
  19.     print AddLowest(3, dice)
  20.  
Jun 1 '07 #18

Smygis
100+
P: 126
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.
Jun 1 '07 #19

P: 52
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:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def role(intList):
  4.     for i in range(len(intList)):
  5.         intList[i] = random.randint(1,6)
  6.  
  7. def AddLowest(nItems, intList):
  8.     return sum(sorted(intList)[0:nItems])
  9.  
  10. dice = [i for i in range(4)]
  11. role(dice)
  12. print dice
  13. print AddLowest(3, dice)
  14.  
  15.  
  16. nTests = 6
  17. for i in range(nTests):
  18.     role(dice)
  19.     print AddLowest(3, dice)
  20.  
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.
Jun 1 '07 #20

bartonc
Expert 5K+
P: 6,596
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:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def role(intList):
  4.     for i in range(len(intList)):
  5.         intList[i] = random.randint(1,6)
  6.  
  7. def AddLowest(nItems, intList):
  8.     return sum(sorted(intList)[0:nItems])
  9.  
  10. dice = [i for i in range(4)]
  11. role(dice)
  12. print dice
  13. print AddLowest(3, dice)
  14.  
  15.  
  16. nTests = 6
  17. for i in range(nTests):
  18.     role(dice)
  19.     print AddLowest(3, dice)
  20.  
Oh.. Ignore the smallest:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def role(intList):
  4.     for i in range(len(intList)):
  5.         intList[i] = random.randint(1,6)
  6.  
  7. def AddLowest(nItems, intList):
  8.     return sum(sorted(intList)[0:nItems])
  9.  
  10. def AddHighest(nItems, intList):
  11.     return sum(sorted(intList)[-nItems:])
  12.  
  13. dice = [i for i in range(4)]
  14. role(dice)
  15. print dice
  16. print AddHighest(3, dice)
  17.  
  18.  
  19. nTests = 6
  20. for i in range(nTests):
  21.     role(dice)
  22.     print AddHighest(3, dice)
  23.  
Jun 1 '07 #21

P: 52
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
Jun 1 '07 #22

Smygis
100+
P: 126
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. for i in range(6):
  4.     dices = []
  5.     for i in range(4):
  6.         dices.append(random.randint(1,6))
  7.     dices.sort()
  8.     temp = 0
  9.     for i in dices[1:]:
  10.         temp += i
  11.     print temp
  12.  
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. for i in range(6):
  4.     dices = [random.randint(1,6) for i in range(4)]
  5.     dices.sort()
  6.     print reduce(lambda x, y: x+y, dices[1:] )
  7.  
is a bit shorter tho
and:
Expand|Select|Wrap|Line Numbers
  1. for i in range(6): print reduce(lambda x, y: x+y, sorted([random.randint(1,6) for i in range(4)])[1:] )
  2.  
is only one line. Totaly insane :D
Jun 1 '07 #23

P: 52
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. for i in range(6):
  4.     dices = [random.randint(1,6) for i in range(4)]
  5.     dices.sort()
  6.     print reduce(lambda x, y: x+y, dices[1:] )
  7.  
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!
Jun 1 '07 #24

ilikepython
Expert 100+
P: 844
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. for i in range(6):
  4.     dices = [random.randint(1,6) for i in range(4)]
  5.     dices.sort()
  6.     print reduce(lambda x, y: x+y, dices[1:] )
  7.  
is a bit shorter tho
and:
Expand|Select|Wrap|Line Numbers
  1. for i in range(6): print reduce(lambda x, y: x+y, sorted([random.randint(1,6) for i in range(4)])[1:] )
  2.  
is only one line. Totaly insane :D
Good job, that's creative. Keep posting!
Jun 1 '07 #25

bartonc
Expert 5K+
P: 6,596
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)
Jun 1 '07 #26

P: 52
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...
Jun 1 '07 #27

Smygis
100+
P: 126
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.
Jun 1 '07 #28

P: 52
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. import random
  3.  
  4. for n in range(6)
  5.     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.
  6.  
  7.     dice.sort()
  8.     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...
  9.     print d2[0}+d2[1]+d2[2] # Could this be written: for n in d2: print n?
  10.  
  11.  
And that's the hard part all done!
Jun 1 '07 #29

Smygis
100+
P: 126
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. import random
  3.  
  4. for n in range(6)
  5.     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.
  6.  
  7.     dice.sort()
  8.     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...
  9.     print d2[0}+d2[1]+d2[2] # Could this be written: for n in d2: print n?
  10.  
  11.  
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.
Jun 1 '07 #30

P: 52
Other then d2[0} shuld be d2[0]
It's a misspelling on my behalf, and I did not misspell it in the actual program, this is only an approximation. I'm impressed that you noticed it though! I didn't.
Jun 1 '07 #31

Smygis
100+
P: 126
It's a misspelling on my behalf, and I did not misspell it in the actual program, this is only an approximation. I'm impressed that you noticed it though! I didn't.
There is much i dont miss.

^^

And you can "overwrite" dice if you want:
Expand|Select|Wrap|Line Numbers
  1. dice = dice[1:]
Not optmal, but it works.
Jun 1 '07 #32

Post your reply

Sign in to post your reply or Sign up for a free account.