0

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

asked Apr 23, 2020 at 11:50
2
  • What is the questions? Also, variable names is not defined. Commented Apr 23, 2020 at 11:53
  • You are comparing with the input function not with its result. Commented Apr 23, 2020 at 11:55

3 Answers 3

1
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.

answered Apr 23, 2020 at 11:54
1
  • 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 entry Commented Apr 23, 2020 at 14:20
1

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))
answered Apr 23, 2020 at 11:55
1

replace this

if input==items:

with this:

if day == items:

and replace this:

if input==stuff:

with this:

if name == staff:
answered Apr 23, 2020 at 11:57
3
  • 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? Commented 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. Commented Apr 23, 2020 at 13:41
  • 1
    not 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() ` Commented Apr 23, 2020 at 14:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.