karan sethi
karan sethi

Reputation: 35

using list in python if statements

I am trying to code a function that gives the user results based on the input. So basically what my function is supposed to do is to make the user input the values for day and based on that proceed towards asking the user their name. If even one of the answers doesn't match the required value, I want my code to be display invalid entry .My code is:

days=[monday, tuesday, wednesday, thursday, friday, saturday, sunday]
def my_function():
    for items in days:
        day=input('what is the day')
        if input==items:
            print('okay')
            for stuff in names:
            name= input("what is your name?")
            if input==stuff:
                print('great')
            else:
                print('invalid entry')  
        else:
            print('invalid entry')

please be gentle as i am new to python. thanks in advance

Upvotes: 0

Views: 152

Answers (3)

Ahmad Girach
Ahmad Girach

Reputation: 96

replace this

if input==items:

with this:

if day == items:

and replace this:

if input==stuff:

with this:

if name == staff:

Upvotes: 1

AKX
AKX

Reputation: 168863

You should probably refactor things so you have a reusable function to have the user choose from a list.


def input_choice(prompt, choices):
    while True:
        response = input(prompt)
        if response in choices:
            return response  # bail out from the infinite loop
        print("invalid entry, try one of %s" % choices)


days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]

day = input_choice("what is the day?", days)
name = input("what is your name?")
print("hi %s, you chose the day %s" % (name, day))

Upvotes: 1

Błotosmętek
Błotosmętek

Reputation: 12927

if input==items:

This should be:

if day==items:

Comparing input, which is a function, to a string makes very little sense.

Smae problem with your next if.

Upvotes: 1

Related Questions