+ 1

Why when i give "Im fine thanks" it gives: "Good"???

y = input("How are u:") if y == "Im fine"or "Good" or "Good!" or "Im fine!" or "Good." or "Im fine." or "I'm fine" or "I'm fine!" or "I'm fine" or "Im good": print("Good") elif y == "Im fine thanks": print("No problem") else: print("What?")

26th Sep 2018, 2:39 PM
Joe
Joe - avatar
4 Antworten
+ 1
А на русском?
26th Sep 2018, 3:16 PM
Joe
Joe - avatar
+ 1
Because you're asking if y==something...and then presenting a bunch of strings that all evaluate to True (without comparing them to anything) say = "moo" if say=="hello" or "mu": print("Condition 1 met") if say=="hello" or say=="mu": print("Condition 2 met") Output: Condition 1 met In the first condition, it's like you wrote this: if (say=="hello") or ("mu"): ... A non-empty string is "truthy" so evaluates as True, while empty strings are "falsy" and evaluate as False.
26th Sep 2018, 3:38 PM
Kirk Schafer
Kirk Schafer - avatar
0
Your multiple ORs make a messy condition check for an interpreter. To make your script work as expected, you need to explicitly compare y with all the answer options. if (y==...) or (y=...) or ... To avoid typing a whole bunch of comparisons, i'd recommend you to use construction like this: y = input("How are u:") if y in ("Im fine", "Good", "Good!", "Im fine!", "Good." ,"Im fine." ,"I'm fine" ,"I'm fine!" , "I'm fine" ,"Im good"): print("Good") elif y == "Im fine thanks": print("No problem") else: print("What?")
26th Sep 2018, 3:10 PM
strawdog
strawdog - avatar
0
Joe У тебя неверно прописаны условия в if - нужно явно сравнивать y с каждой строкой. В противном случае интерпретатор считает что условие всегда истинно и будет постоянно давать один и тот же результат. Я написал, как решить проблему.
26th Sep 2018, 3:52 PM
strawdog
strawdog - avatar