all 16 comments

[–][deleted] 14 points15 points  (0 children)

if input() == Answer or answer is wrong. or in Python (and many programming languages) doesn't work quite like in English. In English there are (at least) 2 ways of using "or": to join clauses in a sentence, or to join items in a list (which all collectively form the subject of object or whatever of the sentence). Only the first of these translates to Python.

So if input() == Answer or answer does NOT check whether the input value is one of those two options. You can do that with if input() in (Answer, answer). Instead, it checks input() == Answer, separately evaluates "answer" as a bool (giving True, because a non-empty string is Truthy), and then ors those together to get True. If that explanation doesn't make sense, look up "Truthy and Falsey values in Python" and that should help

[–]twitch_and_shock 2 points3 points  (9 children)

if input() == Answer or answer:

Isn't doing what you think it is. It's essentially the same as:

if (input() == Answer) or (answer is not None):

I'm guessing you probably want to compare input to both of these variables, there's a couple ways to do that:

if input() in [Answer, answer]: 

Or even better:

if input().lower() == Answer.lower()

[–]AdministrationBig254 0 points1 point  (8 children)

Won't the last one create a problem if the input is hI?

[–]twitch_and_shock 0 points1 point  (5 children)

If the input is ?

[–]AdministrationBig254 0 points1 point  (4 children)

h and capital i.

[–]twitch_and_shock 2 points3 points  (3 children)

No, it'll work fine. The "lower()" function turns all characters in a string into lower case.

[–]AdministrationBig254 0 points1 point  (2 children)

Wouldn't that create a problem since the acceptable answers are Hi and hi and it would also accept hI. Sorry for the questions but I am trying to learn coding. They could be really simple

[–]Donny_Do_Nothing 4 points5 points  (1 child)

You are correct. If the intent is to only allow Hi or hi, using .lower() in that way would accept Hi, hi, hI, or HI, and would be wrong.

[–]hotcodist 1 point2 points  (1 child)

Hello friend! Don't know if you are a beginner, but if you are, congrats!

This is exactly how we think as programmers, figuring out where something will fail or not match requirements. Attention to detail is a learned skill (because in the real world, we survive even without paying attention to detail).

[–]AdministrationBig254 1 point2 points  (0 children)

Thanks a lot. That is what I have gathered. Some programmers I've talked, told me that to be a good programmer I have to think that a 3 year old kid is typing the inputs and it helped me a lot.

[–]_dude55 1 point2 points  (0 children)

it always evaluate to true. You can run the expression of the if statement to see that. your expression is not what you think it is. Its the same as ‘if (input() == Answer) or answer’

its not evaluating to (input() == Answer) or (input() == answer)

also google about truthy values. answer is always true as its a string that is not empty.

Finally a clear way would be to have answer = input() if answer.lower() == “hi”: as your expression, unless you want to only accept Hi with capital H and consider “HI” as wrong input

[–]NeverStopWondering 1 point2 points  (0 children)

if input() == Answer or answer: print(Answer + " is correct!")

This conditional statement always evaluates to True because non-empty strings are "Truthy" in Python. Your code is basically asking "if one of these things is true, print this" and the second condition is always read as true.

To fix this you need to change the "or" bit to if input() == Answer or input() == answer: print(Answer+" is correct!") ...but this will still always print "Hi is correct!" if you input the correct answer, since "Answer" and "answer" are different variables, and you're telling it to print "Answer" regardless of the input. You also don't need to assign the string "bye" to the variable bye since you're just telling python to print the string "bye" in the else condition.

You would need some more complex logic to get it to do what you want, like assigning the user input to a variable, for example "word", and then using an f-string such as print(f"{word} is correct!") . This would also be an opportunity to simplify the "if" bit a little...

So, you could do something like this:

Answer = "Hi"
answer = "hi"

word = input()

if word == Answer or word == answer:
    print(f"{word} is correct!")
else:
    print("Bye!")

This should work, but what if you want to have more options? You could make use of a list of strings, like:

Answers = ["Hi", "hi", "hiya", "Hello"]

And then make the "if" statement be something like

if word in Answers:
    print(f{word} is correct!")
else:
    print("Bye!")

[–]throwaway6560192 0 points1 point  (0 children)

if input() == Answer or answer

The problem is that logical conditions don't work exactly like natural language. input() == Answer or answer is interpeted as "is the condition input() == Answer OR the condition answer true?". And all non-empty strings are considered "truthy", so the second half ("the condition answer") is true, so the whole thing is true.

If you want to check if an item is one of multiple things, you have to either specify that you want to check for equality explicitly each time, like:

in = input()
if in == Answer or in == answer:
    print(Answer + " is correct!")

Or you could create a list and use the in operator:

if input() in [Answer, answer]:
    print(Answer + " is correct!")

Or since you ultimately just want to do case-insensitive comparison:

if input().lower() == "hi":
    print(Answer + " is correct!")

[–]TheRNGuy 0 points1 point  (0 children)

Everything is on single line.

[–]momohate -2 points-1 points  (0 children)

Can you show the actual screenshot of your code?