Advertisement
kingtigermusic

dice roller

Jun 14th, 2024 (edited)
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.90 KB | Source Code | 0 0
  1. from numpy import random
  2. import sys
  3.  
  4. ### This program simulates rolling dice by generating random numbers based upon user input.
  5. ### It "rolls" 1-10 dice of 2-100 sides.
  6. ### It can be run with no arguments, one argument or two arguments.
  7. ### With no arguments, it rolls one twenty-sided die.
  8. ### With one argument {input1}, it rolls {input1} twenty-sided dice, unless {input1} starts with the letter "d", in which case it rolls one {input1}-sided die.
  9. ### With two arguments {input1} & {input2}, it rolls {input1} {input2}-sided dice.
  10. ### If either input is "infinity" or same variant, the program instead returns a humorous result indicating some sort of space-time disturbance, as long as the arguments are otherwise in rage (e.g. the user can't roll 20 inf-sided dice).
  11. ### Additional special rsesponses can be added, such as if a user tries to roll a one-sided die, i.e. "There's no such thing as a one-sided die, silly."
  12.  
  13.  
  14. ### TODO: Replace lengthy "if / elif"s with switch cases ###
  15.  
  16.  
  17.  
  18.  
  19. # List of phrases to return if an infinite-sided die is rolled
  20. infresults = ["a black hole. Hey, I think CERN is calling.", "turned into a pony. Yay?", "teleported to the Gamma Quadrant. See you in 7 years!"]
  21.  
  22.  
  23.  
  24.  
  25. ### Beginnging of main program ###
  26.  
  27. # Set defaults within the program itself
  28. def dice(amount = 1, sides = 20):
  29.  
  30.     # always print this line first
  31.     def initialprint():
  32.         print(f"You rolled {amount} D{sides} and got... ")
  33.  
  34.     # check that "amount" is in range and double check sanitation
  35.     if amount >= 1 and amount <= 10 and type(amount) is int:
  36.  
  37.         # infinite-sided die
  38.         if type(sides) == str:
  39.             if sides == "inf" or sides == "infinity" or sides == "∞" or sides == "infinite":
  40.                 initialprint()
  41.                 print(infresults[random.randint(len(infresults))])
  42.             else:
  43.                 user_error()
  44.  
  45.         # check that "sides" is in range and double check sanitation
  46.         elif type(sides) is int:
  47.             if sides >= 2 and sides <= 100:
  48.                 initialprint()
  49.  
  50.                 # Format natural language syntax & keep output on one line, and generate & print results
  51.                 if amount > 2:
  52.                     for amount in range(amount - 1):
  53.                         print(f"a {random.randint(sides) + 1},", end=" ", flush=True)
  54.                     print(f"and a {random.randint(sides) + 1}.")
  55.                 elif amount == 2:
  56.                     for amount in range(amount - 1):
  57.                         print(f"a {random.randint(sides) + 1}", end=" ", flush=True)
  58.                     print(f"and a {random.randint(sides) + 1}.")
  59.                 else:
  60.                     print(f"a {random.randint(sides) + 1}.")
  61.             else:
  62.                 user_error()
  63.         else:
  64.             user_error()            
  65.     else:
  66.         user_error()
  67.     prog_end()
  68.  
  69. ### End of main program ###
  70.  
  71.  
  72.  
  73.  
  74. ### Beginning of simple functions ###
  75.  
  76. # Print generic error; can add more types of errors or outputs if needed
  77. def user_error():
  78.     print("That doesn't work. Please use 1-10 dice of 2-100 sides. If you enter only one number, it will be used as the number of dice, unless you put a \"d\" in front. Type \"!rollhelp\" for examples.")
  79.     prog_end()
  80.  
  81. # Terminate the program; can add output or other functions if needed
  82. def prog_end():
  83.     sys.exit()
  84.  
  85. # Attempt to convert initial arguments into integers
  86. def makeint(inp):
  87.     try:
  88.         inp = int(inp)
  89.         return inp
  90.     except:
  91.         user_error()
  92.  
  93. ### End of simple functions ###
  94.  
  95.  
  96.  
  97.  
  98. ### Beginning of input sanitizer ###
  99.  
  100. # Parse the initial arguments and set defaults
  101. if len(sys.argv) == 2:
  102.     input1 = sys.argv[1]
  103.     input2 = "null"
  104. elif len(sys.argv) == 3:
  105.     input1 = sys.argv[1]
  106.     input2 = sys.argv[2]
  107. elif len(sys.argv) > 3:
  108.     user_error()
  109. elif len(sys.argv) < 2:
  110.     input1 = 1
  111.     input2 = 20
  112.  
  113. # Various checks for the first argument & pass first argument to second argument if needed
  114. if type(input1) == str:
  115.     input1 = input1.lower()
  116.     if input1[0] == "d":
  117.         input2 = input1[1:]
  118.         input1 = 1
  119.     elif input1 == "inf" or input1 == "infinity" or input1 == "∞" or input1 == "infinite":
  120.         if input2 == "null":
  121.             input2 = input1
  122.             input1 = 1
  123.         else:
  124.             user_error()
  125.     else:
  126.         input1 = makeint(input1)
  127.         if input2 == "null":
  128.             input2 = 20
  129. else:
  130.     input1 = makeint(input1)
  131.  
  132. # Various checks for second argument
  133. if type(input2) == str:
  134.     input2 = input2.lower()
  135.     if input2[0] == "d":
  136.         try:
  137.             input2 = int(input2[1:])
  138.         except:
  139.             user_error()
  140.     elif input2 == "inf" or input2 == "infinity" or input2 == "∞" or input2 == "infinite":
  141.         pass
  142.     else:
  143.         input2 = makeint(input2)
  144. else:
  145.     input2 = makeint(input2)
  146.  
  147. ### End of sanitizer ###
  148.  
  149.  
  150.  
  151.  
  152. # Execute main program
  153. dice(input1, input2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement