473,686 Members | 3,531 Online

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

52 New Member
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
31 1991
Smygis
126 New Member
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
6,596 Recognized Expert Expert
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
2,851 Recognized Expert Moderator Specialist
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
6,596 Recognized Expert Expert
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
6,596 Recognized Expert Expert
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
126 New Member
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
6,596 Recognized Expert Expert
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
6,596 Recognized Expert Expert
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
6,596 Recognized Expert Expert
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