Python checks if a string is one of a specific set of values

I am learning python on codecademy and my current task is this:

Write a shut_down function that takes one parameter (you can use whatever you like; in this case we will use s for the string). The closing function should return "Shutdown ..." when it receives "Yes" , "Yes" , or "YES" as an argument, and "Shutdown is interrupted!" when it receives "No", "No" or "NO" .

If it receives anything other than these inputs, the function should return "Sorry, I did not understand you."

It was easy for me, but I still can’t do it.

My code I did to test the function:

def shut_down(s): if s == "Yes" or s == "yes" or s == "YES": return "Shutting down..." elif s == "No" or "no" or "NO": return "Shutdown aborted!" else: return "Sorry, I didn't understand you." i = input("Do you want to shutdown?") print(i) #was to test the input print(shut_down(i)) #never returns "Sorry, I didn't understand you" 

It works great for no and yes, but for some reason, if I put a space before any yes or even if I just type "a", it prints "Shutdown aborted!". although he should print "Sorry, I did not understand you."

What am I doing wrong?

+4
source share
3 answers

You forgot to write s == "no" in your first elif:

 def shut_down(s): if s == "Yes" or s == "yes" or s == "YES": return "Shutting down..." elif s == "No" or "no" or "NO": # you forgot the s== in this line return "Shutdown aborted!" else: return "Sorry, I didn't understand you." 

Do it:

 def shut_down(s): if s == "Yes" or s == "yes" or s == "YES": return "Shutting down..." elif s == "No" or s == "no" or s == "NO": # fixed it return "Shutdown aborted!" else: return "Sorry, I didn't understand you." 

This is because:

 elif s == "No" or "no" or "NO": #<---this elif s == "No" or True or True: #<---is the same as this 

Since this is an accepted answer, I’ll take a closer look to include standard methods: an agreement to compare strings regardless of capitalization (equalsIgnoreCase) is to use .lower() like this

 elif s.lower() == "no": 
+10
source

Instead of checking for different capitalization combinations, you can use the lower function to return a copy of s in lower case and compare with that.

 def shut_down(s): if s.lower() == "yes": return "Shutting down..." elif s.lower() == "no": return "Shutdown aborted!" else: return "Sorry, I didn't understand you." 

It is much simpler and easier to debug. Alternatively, you can also use upper and compare with "YES" and "NO" .


If this does not help due to the coincidence of cases like nO , I would go with the in statement:

 def shut_down(s): if s in ("yes","Yes","YES"): return "Shutting down..." elif s in ("no","No","NO"): return "Shutdown aborted!" else: return "Sorry, I didn't understand you." 
+6
source

Python evaluates non-empty lines as True , so your elif condition is always evaluated as True .

 >>> bool('No') True >>> bool('NO') True 

Performing a logical or with a value of True will always return True , so it never reaches the else condition and gets stuck in elif .

You need to check the conditions using.

elif choice == 'no' or choice == 'NO' or choice == 'No':

EDIT . As pointed out in a glglgl comment, == bound more strongly than or , so your condition evaluates to (s == 'No') or 'no' or 'NO' , not s == ('No' or 'no' or 'NO') , in which case you are in the else part even for user input 'NO' .

+4
source

Source: https://habr.com/ru/post/1493854/


All Articles