# Python User Input

In our adventure game we may want to ask the players questions and let them interact with the game.  Until now the game does stuff without regard tot he players.  Kind of weird for a game!  So now we will ask the players which die they would like to roll and also ask the number of times they would like to roll it.

To accomplish this we will use x = input(“Here is my question to the player”)

In our example we are asking the user for a number so we must convert it to an integer

x = int(input(“Here is my question to the player”))  #the int converts this to an integer

The x stores the value so we may keep using the data.

Here we go…

```import random

#4 sided die roll
def roll4(choice, rolls):
for i in range(0,rolls): #number of times to roll the dice
print("Four Sided Die Roll: ", random.randint(1, 4))

#6 sided die roll
def roll6(choice, rolls):
for i in range(0,rolls): #number of times to roll the dice
dice = [1, 2, 3, 4, 5, 6]
playerRoll = random.choice(dice)
print('Six Sided Die Roll: ', playerRoll)

def roll20(choice, rolls):
for i in range(0,rolls): #number of times to roll the dice
print("Twenty Sided Die Roll: ", random.randint(1, 20))

def main():
dieChoice = int(input("Which die would you like to roll?:  (4, 6 or 20) "))
numRolls = int(input("How many times would you like to roll it?: "))

if dieChoice == 4:
roll4(dieChoice, numRolls)
elif dieChoice == 6:
roll6(dieChoice, numRolls)
elif dieChoice == 20:
roll20(dieChoice, numRolls)
else:
print("Not a valid number.")

if __name__ == '__main__':main()```

So we create the functions that will calculate the random die rolls.  Then we have a main() function that uses an if/else statement to sort the choices made by the user.

Now we may want to validate the input from the user to make sure it fits our needs.  If the user enters their name instead of a number it would be nice to ask the user again, instead of crashing the program.

Look this over but not get hung up if this confuses you.

while True = always True infinite loop must be broken with break

try = try this and hope you do not get an error message

except = you got an error message, but rather than crash the program do these things

```import random

#4 sided die roll
def roll4(choice, rolls):
for i in range(0,rolls): #number of times to roll the dice
print("Four Sided Die Roll: ", random.randint(1, 4))

#6 sided die roll
def roll6(choice, rolls):
for i in range(0,rolls): #number of times to roll the dice
dice = [1, 2, 3, 4, 5, 6]
playerRoll = random.choice(dice)
print('Six Sided Die Roll: ', playerRoll)

def roll20(choice, rolls):
for i in range(0,rolls): #number of times to roll the dice
print("Twenty Sided Die Roll: ", random.randint(1, 20))

def main():
while True:  #True is always true infinite loop
try:
dieChoice = int(input("Which die would you like to roll?:  (4, 6 or 20) "))
numRolls = int(input("How many times would you like to roll it?: "))

if dieChoice == 4:
roll4(dieChoice, numRolls)
elif dieChoice == 6:
roll6(dieChoice, numRolls)
elif dieChoice == 20:
roll20(dieChoice, numRolls)
else:
print("You entered an incorrect dice choice (must be 4, 6 or 20)")
continue
if numRolls != abs(numRolls):
print("You must enter a positive integer")
continue
except:
print("You entered an incorrect number of rolls, must be integer.")
continue
else:
break

if __name__ == '__main__':main()```

Hopefully this was easy!  If not do not fret and carry on!