I have created a program which rolls a die according to the number of sides that the user enters:
def var():
import random
dicesides = int(input("Please enter the amount of sides you want the dice that is being thrown to have. The dice sides available are: 4, 6 and 12: "))
script(dicesides, random)
def script(dicesides, random):
if dicesides == 4:
dice4 = int(random.randrange(1, dicesides))
print(dicesides, " sided dice, score", dice4)
elif dicesides == 6:
dice6 = int(random.randrange(1, dicesides))
print(dicesides, " sided dice, score", dice6)
elif dicesides == 12:
dice12 = int(random.randrange(1, dicesides))
print(dicesides, " sided dice, score", dice12)
elif dicesides != 4 or dicesides != 6 or dicesides != 12:
print("That number is invalid. Please try again.")
var()
repeat = str(input("Repeat? Simply put yes or no : "))
if repeat == "yes":
var()
else:
quit()
var()
Is there a way to shorten this?