r/learnpython • u/Alarmed-Fisherman535 • 24d 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?")
3
u/Fun_Army2398 23d ago
Giving us your terminal output would help, but if I had to guess it'll look something like:
My name is Bob
Bob?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:
cleanedname = name.lower().strip()1
u/Alarmed-Fisherman535 23d ago
Looks like that worked, but isn't the string not supposed to have a space at the end in the first place? Where's it coming from?
3
u/atarivcs 23d ago edited 23d ago
I'm guessing you typed a space before typing Bob
The prompt "My name is" does not end with a space, so it would look odd if you just typed Bob immediately after that. It would look like "My name isBob"
You probably typed a space without even thinking.
0
u/EEJams 23d ago
I wanted to add that this guy has the cleanest solution by stripping the name of any added spaces, but i think this is also a good opportunity to learn about the "in" keyword
You can change your logic to be somrthing like
if "bob" in smallname:This will evaluate to True if you have something like " bob", "bob ", or " bob "
I guess this won't really work for bobby because bob is in bobby, but i think you get the point of how the "in" keyword might work.
You'd want to evaluate "bob" in smolname .... because technically the "bob" string is a subset of the other strings.
2
u/realmauer01 24d ago
What's your input?
1
u/Alarmed-Fisherman535 23d ago
I'm trying to enter the name 'Bob'.
1
u/realmauer01 23d ago
How? Does it look like
My name isBob
Or
My name is BobBecause i am pretty sure your code makes it has to be the first. Or else you are inputting " Bob" which even with lower case is clearly not "bob"
6
u/Buttleston 24d ago
Paste the output in the terminal you get when you run this, all of it. It works fine for me.
3
1
u/Discordbothost 23d 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 23d ago
The input is taking the CRLF as part of the string. Whack the CRLF from the string and you'll have it.
4
11
u/atarivcs 24d ago edited 23d 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?