473,598 Members | 3,141 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1988
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
with a class and doc strings thrown in for free!
Jun 1 '07 #10

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

Similar topics

1
1950
by: lawentzel | last post by:
I am new to PHP and am an avid RPG gamer. I am looking to use PhpMyChat to be able to game with friends all over the US. With that having been said, PhpMyChat seems to offer a lot of what I am looking for. It allows you to send a message to individual players (chatters). It allows for actions using "/me". The only thing it really needs is the capability to do a dice roll. Can someone point me to a site which might have some examples...
101
6345
by: Elijah Cardon | last post by:
Let's say I have m dice having n sides, such that n^m is not going to bust int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1, 2}. What is a good way to represent this in c so that I can check cases by brute force? EC
2
3750
by: DeFault | last post by:
I have this assigment to do that determines which of three dice are loaded. The dice are actually three C functions called dieA(), diaB(), and dieC()each of which returns an int in the range 1 – 6 when called. I would have to exercise each of the dice functions and determines which one is “loaded.” This is my idea of how to make ten throws of die A #include "threeDice.h" /* header file */ int main() {
1
1321
by: Ronald S. Cook | last post by:
So we'll have a SelectPens function and associated proc to bring back all pens to the client. Now think about all the variations that might be needed (many). For example, to show the pen feed sequence I'll need a subset of fields and ordered by FeedSequence. Option 1) I've seen some shops where they have a function (and associated proc) for every slice and dice.
5
7739
by: Steve | last post by:
This simple script works first time but if you click a second time on the button nothing happens. I expected that the roll function would be executed again but it doesn't. Could someone explain why. http://members.chello.at/stephen.joung/testsite/hotspots1.html javascript is at http://members.chello.at/stephen.joung/testsite/hotspots1.js Thanks in advance
0
8393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8048
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8263
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5848
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5438
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3896
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1503
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1246
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.