r/learnpython • • 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

14 comments sorted by

3

u/Fun_Army2398 18d 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 18d 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 18d ago edited 18d 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 18d 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 18d ago

What's your input?

1

u/Alarmed-Fisherman535 18d ago

I'm trying to enter the name 'Bob'.

1

u/realmauer01 18d ago

How? Does it look like

My name isBob
Or
My name is Bob

Because 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"

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?

1

u/Sehzat_Davoyan 18d ago

good catch on the prompt spacing, invisible whitespace is the worst when youre just starting out

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.

4

u/atarivcs 18d ago

input() doesn't do that.