Skip to content Skip to sidebar Skip to footer

Obtaining User Input In A Python Program

I'm new to Python. Let's say I want to create a input that is something like this Do you want a Porsche / y . n: n Do you want a Ferrari / y . n: y Do you want a Lamborghini / y .

Solution 1:

list = ["Lambo", "Ferrari", "Kia"]
responses = []
for car inlist:
  choice = input("Do you want {} y or n?".format(car))
  responses.append(choice)

for i inrange(0, len(responses)):
  if responses[i] == 'y':
    print("Ok, you can have choice {}".format(i+1))

You can create a list of all your 7 cars and record user responses for each car. After collection all responses, you can check if the user responded yes for a particular response and display it.

Below is the output when I run it:

Do you want Lambo y or n?n
Do you want Ferrari y or n?y
Do you want Kia y or n?n
Ok, you can have choice 2

Hope it helps!

Solution 2:

You can do this without duplicating code:

cars = ['car1', 'car2', 'car3', 'etc']

ans = []
for i inrange(len(cars)):
    ans.append(input(f'Do you want {cars[i]}? (y/n) '))

for i inrange(len(cars)):
    if ans[i].lower() == 'y':
        print(f'Ok,  your chose is {i+1}')

Solution 3:

ferrari =input("do you want a ferrari? y/n")
if ferrari=="y":
    print("here is your ferrari")
if ferrari=="n":
    lambor=input("lanborgini then?")
    if lambor == "y":
        print("here is your lamborgini")
    if lambor == "n":
        print("keep with the another cars")

Or you can try this

    def Cars(what):
    what = input("do you wanna a " + what)
    if what=="y":
        print("ookey!")
    if what=="n":
        print("we have more cars")
Cars("ferrari")
Cars("peudgeot")
Cars("mcqueen")
Cars("corsa")
Cars("nissan")

#you can add more cars#

Solution 4:

Just add more to the list "cars" to ask the user their response to other cars.

userChoice = ''
cars = ['Porsche', 'Ferrari', 'Lamborghini']
c = 0while(userChoice != 'y'and c < len(cars)):
    chooseCar = cars[c]
    userChoice = input(f"Do you want a {chooseCar} / y . n: ")
    c += 1if(c == len(cars)):
  print("You don't want any of the cars.")
else:
  print(f"Ok, you can have choice {c}")

Also this I'm checking if the counter "c" is less than the length of the list because it will crash of there are no more cars to reference.

Solution 5:

The simple way is to use list and get the index of selected car. Since Python starts at index zero, we need to add one(Python 3.6+ using f-format):

cars_models = ['Porsche', 'Ferrari', 'Lamborghini']

for car in cars_models:
    is_wanted = input(f'Do you want a {car} / y . n: ')

    if is_wanted == 'y':
        car_wanted = car

selected_choice = cars_models.index(car_wanted)

print(f'Ok, you can have choice {selected_choice +1}')

We first create a list of cars we want to check. Then we loop with questions. If the answer is y, we record which car. At the end we find the location of wanted car.

Before you celebrate, we have some issues we could take care. These are what-ifs: what if the user selects all n. Our current script will crash as car_wanted will be referenced before assignment. Solutions: Ask forgiveness > permission

# ...try:
    select_choice = cars_models.index(car_wanted)
    print(f'Ok, you can have choice {select_choice +1}')
except UnboundLocalError:
    print('Hmm, you have no choice')

Okay, this will catch that error. We are not done. What if the use answer (N/No/no instead of n, Y/Yes/yes instead of y)? What should we do when they input something completely out of scope. E.g. 'quit'?

As a programmer you need to try see these issues and address them.

Post a Comment for "Obtaining User Input In A Python Program"