Skip to content Skip to sidebar Skip to footer

Variables Based On Input

Python Version=3.5 So I would like to know how I can set variables based on the input from the user. For example if a user was to answer 7 to this: residents=input('How many peopl

Solution 1:

def get_int(prompt):
    while True:
        try:
            return int(input(prompt))
        except ValueError:   # not an int!
            pass   # try again

residents = get_int("How many people live at your house? ")

Edit: rather than having named variables for each person, you can use a list:

resident_names = [input("Name of resident {}: ".format(i)) for i in range(1, residents + 1)]

Post a Comment for "Variables Based On Input"