r/learnpython • u/Alarmed-Fisherman535 • 18d ago
Feeling incredibly dumb right now. What am I missing?
EDIT: Thanks for the help, folks!
Why is it not printing "Bob!" and going right to the else? I feel like it's something super obvious but I really can't find anything wrong.
name = input("My name is")
print (name)
smallname = name.lower()
print(smallname)
if smallname == "bob":
print("Bob!")
else:
print("Bob?")
0
Upvotes
3
u/Fun_Army2398 18d ago
Giving us your terminal output would help, but if I had to guess it'll look something like:
Right?
If so it's because the string you pass to input doesn't have a space at the end, so if you type the space before the name you pass, the space gets added to the name and
if " bob" == "bob"is false. To make the output look pretty just add a space at the end of"My name is", however to prevent any issues like this, you can add the strip method to your input processing like:2
u/realmauer01 18d ago
What's your input?
11
u/atarivcs 18d ago edited 18d ago
Well, the obvious reason would be that you didn't enter Bob at the prompt.
Did you put quotes around it or something?
I see that the prompt "My name is" does not end with a space. Did you enter a space before typing Bob?
6
u/Buttleston 18d ago
Paste the output in the terminal you get when you run this, all of it. It works fine for me.
3
u/Sparklepaws 18d ago
What is your input?
1
u/Discordbothost 18d ago
almost always trailing whitespace from input(). try smallname = name.strip().lower() — if you typed " Bob" (space after the prompt) then smallname was " bob" which is not "bob". print(repr(smallname)) is the quick way to see the invisible chars.
-2
u/chuzohga 18d ago
The input is taking the CRLF as part of the string. Whack the CRLF from the string and you'll have it.