Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from numpy import random
- import sys
- ### This program simulates rolling dice by generating random numbers based upon user input.
- ### It "rolls" 1-10 dice of 2-100 sides.
- ### It can be run with no arguments, one argument or two arguments.
- ### With no arguments, it rolls one twenty-sided die.
- ### 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.
- ### With two arguments {input1} & {input2}, it rolls {input1} {input2}-sided dice.
- ### 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).
- ### 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."
- ### TODO: Replace lengthy "if / elif"s with switch cases ###
- # List of phrases to return if an infinite-sided die is rolled
- 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!"]
- ### Beginnging of main program ###
- # Set defaults within the program itself
- def dice(amount = 1, sides = 20):
- # always print this line first
- def initialprint():
- print(f"You rolled {amount} D{sides} and got... ")
- # check that "amount" is in range and double check sanitation
- if amount >= 1 and amount <= 10 and type(amount) is int:
- # infinite-sided die
- if type(sides) == str:
- if sides == "inf" or sides == "infinity" or sides == "∞" or sides == "infinite":
- initialprint()
- print(infresults[random.randint(len(infresults))])
- else:
- user_error()
- # check that "sides" is in range and double check sanitation
- elif type(sides) is int:
- if sides >= 2 and sides <= 100:
- initialprint()
- # Format natural language syntax & keep output on one line, and generate & print results
- if amount > 2:
- for amount in range(amount - 1):
- print(f"a {random.randint(sides) + 1},", end=" ", flush=True)
- print(f"and a {random.randint(sides) + 1}.")
- elif amount == 2:
- for amount in range(amount - 1):
- print(f"a {random.randint(sides) + 1}", end=" ", flush=True)
- print(f"and a {random.randint(sides) + 1}.")
- else:
- print(f"a {random.randint(sides) + 1}.")
- else:
- user_error()
- else:
- user_error()
- else:
- user_error()
- prog_end()
- ### End of main program ###
- ### Beginning of simple functions ###
- # Print generic error; can add more types of errors or outputs if needed
- def user_error():
- 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.")
- prog_end()
- # Terminate the program; can add output or other functions if needed
- def prog_end():
- sys.exit()
- # Attempt to convert initial arguments into integers
- def makeint(inp):
- try:
- inp = int(inp)
- return inp
- except:
- user_error()
- ### End of simple functions ###
- ### Beginning of input sanitizer ###
- # Parse the initial arguments and set defaults
- if len(sys.argv) == 2:
- input1 = sys.argv[1]
- input2 = "null"
- elif len(sys.argv) == 3:
- input1 = sys.argv[1]
- input2 = sys.argv[2]
- elif len(sys.argv) > 3:
- user_error()
- elif len(sys.argv) < 2:
- input1 = 1
- input2 = 20
- # Various checks for the first argument & pass first argument to second argument if needed
- if type(input1) == str:
- input1 = input1.lower()
- if input1[0] == "d":
- input2 = input1[1:]
- input1 = 1
- elif input1 == "inf" or input1 == "infinity" or input1 == "∞" or input1 == "infinite":
- if input2 == "null":
- input2 = input1
- input1 = 1
- else:
- user_error()
- else:
- input1 = makeint(input1)
- if input2 == "null":
- input2 = 20
- else:
- input1 = makeint(input1)
- # Various checks for second argument
- if type(input2) == str:
- input2 = input2.lower()
- if input2[0] == "d":
- try:
- input2 = int(input2[1:])
- except:
- user_error()
- elif input2 == "inf" or input2 == "infinity" or input2 == "∞" or input2 == "infinite":
- pass
- else:
- input2 = makeint(input2)
- else:
- input2 = makeint(input2)
- ### End of sanitizer ###
- # Execute main program
- dice(input1, input2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement