Click here to Skip to main content
15,069,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am bored, and of course, when you're bored and know how to code, you just code whatever you can think of. I thought of making a die-rolling application, and the user can choose to roll anywhere from a 1 sided die to a 50 sided die. I tried out the code and entered a 50 sided die, but the random number generated was 60. What happened here?

Code:
Python
import time
import random
while True:
  sides = input("Please input an amount of sides for your die: ")
  print("%s sided die\n\n" % sides)
  time.sleep(1)
  print("Rolling...")
  time.sleep(4)
  if sides == ("1"):
    onetoone = (random.randint(1,1))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("2"):
    onetoone = (random.randint(1,2))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("3"):
    onetoone = (random.randint(1,3))
    print("Your rolled number is %o\n\n" % onetoone)
    
  elif sides == ("4"):
    onetoone = (random.randint(1,4))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("5"):
    onetoone = (random.randint(1,5))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("6"):
    onetoone = (random.randint(1,6))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("7"):
    onetoone = (random.randint(1,7))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("8"):
    onetoone = (random.randint(1,8))
    print("Your rolled number is %o\n\n" % onetoone)
    
  elif sides == ("9"):
    onetoone = (random.randint(1,2))
    print("Your rolled number is %o\n\n" % onetoone)
    
  elif sides == ("10"):
    onetoone = (random.randint(1,10))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("11"):
    onetoone = (random.randint(1,11))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("12"):
    onetoone = (random.randint(1,12))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("13"):
    onetoone = (random.randint(1,13))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("14"):
    onetoone = (random.randint(1,14))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("15"):
    onetoone = (random.randint(1,15))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("16"):
    onetoone = (random.randint(1,16))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("17"):
    onetoone = (random.randint(1,17))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("18"):
    onetoone = (random.randint(1,18))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("19"):
    onetoone = (random.randint(1,19))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("20"):
    onetoone = (random.randint(1,20))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("21"):
    onetoone = (random.randint(1,21))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("22"):
    onetoone = (random.randint(1,22))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("23"):
    onetoone = (random.randint(1,23))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("24"):
    onetoone = (random.randint(1,24))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("25"):
    onetoone = (random.randint(1,25))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("26"):
    onetoone = (random.randint(1,26))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("27"):
    onetoone = (random.randint(1,27))
    print("Your rollfed number is %o\n\n" % onetoone)
  
  elif sides == ("28"):
    onetoone = (random.randint(1,28))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("29"):
    onetoone = (random.randint(1,29))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("30"):
    onetoone = (random.randint(1,30))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("31"):
    onetoone = (random.randint(1,31))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("32"):
    onetoone = (random.randint(1,32))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("33"):
    onetoone = (random.randint(1,33))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("34"):
    onetoone = (random.randint(1,34))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("35"):
    onetoone = (random.randint(1,35))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("36"):
    onetoone = (random.randint(1,36))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("37"):
    onetoone = (random.randint(1,37))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("38"):
    onetoone = (random.randint(1,38))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("39"):
    onetoone = (random.randint(1,39))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("40"):
    onetoone = (random.randint(1,40))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("41"):
    onetoone = (random.randint(1,41))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("42"):
    onetoone = (random.randint(1,42))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("43"):
    onetoone = (random.randint(1,43))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("44"):
    onetoone = (random.randint(1,44))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("45"):
    onetoone = (random.randint(1,45))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("46"):
    onetoone = (random.randint(1,46))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("47"):
    onetoone = (random.randint(1,47))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("48"):
    onetoone = (random.randint(1,48))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("49"):
    onetoone = (random.randint(1,49))
    print("Your rolled number is %o\n\n" % onetoone)
  
  elif sides == ("50"):
    onetoone = (random.randint(1,50))
    print("Your rolled number is %o\n\n" % onetoone)
  
  else:
    print("Sorry, that number is too big. (MAX 50)")


What I have tried:

I have been looking up how this is possible, but no site that I go to has the information that I'm looking for.
Posted
Updated 7-Jan-21 23:02pm
v3

1 solution

You are using the octal format specifier %o while you should use the decimal one %d.
For example, the (decimal) number 50 becomes by 62 = 6*8+2 in octal representation.

By the way, you know, you could write your program in a more compact way:
Python
import time
import random
while True:
  sides = int(input("Please input an amount of sides for your die: "))
  if  sides < 1 or  sides > 50:
    break

  print("%d sided die\n\n" % sides)

  time.sleep(1)
  print("Rolling...")
  time.sleep(4)
  onetoone = (random.randint(1,sides))
  print("Your rolled number is %d\n\n" % onetoone)
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900