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
3 Answers 3
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
.
-
hi i am getting this error where if i put in monday as the first value it seems to work( i can then put in the subsequent values and it will work), however, if i start somewhere in the middle it returns invalid entrykaran sethi– karan sethi04/23/2020 14:20:45Commented Apr 23, 2020 at 14:20
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))
replace this
if input==items:
with this:
if day == items:
and replace this:
if input==stuff:
with this:
if name == staff:
-
hi, thanks alot for your answer! can you please also tell me why my loop wont end even after the user types in something invalid?karan sethi– karan sethi04/23/2020 13:33:36Commented Apr 23, 2020 at 13:33
-
there's also an indentation issue in your code. i.e. for stuff in names: name= input("what is your name?") it's in same indent. However it shouldn't.Ahmad Girach– Ahmad Girach04/23/2020 13:41:48Commented Apr 23, 2020 at 13:41
-
1not sure whether you want this. but try this: ` days=['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'] names = ['Patrick', 'Jonathan', 'Sara', 'Maria'] def my_function(): day = input('what is the day') if day in days: print('okay') user_name = input("what is your name?") if user_name in names: print('great') else: print('invalid entry') else: print('invalid entry') my_function() `Ahmad Girach– Ahmad Girach04/23/2020 14:39:27Commented Apr 23, 2020 at 14:39
names
is not defined.input
function not with its result.