The Student Room Group

Help with Computing Homework?

Hey, I'm trying to write some Python code, but I've messed up. I wrote this:

name = input("What is your name?")
print("Hello %s! Nice to meet you!" % name)
response = input("How many letters are in your name?")
length = len(name)
if response == length :
print("That's right!")
else:
print("No, the length of your name is %i!" % length)

but when I run it, it does this:

What is your name? leo
Hello leo! Nice to meet you!
How many letters are in your name? 3
No, the length of your name is 3!

Does anybody know where I went wrong?
You're comparing a string with an integer:
"3" == 3 will evaluate to False.

Also, using the printf-style of string formatting with % symbols is really old and ugly. Look up how to do f-strings.
Reply 2
Original post by Composure
You're comparing a string with an integer:
"3" == 3 will evaluate to False.

Also, using the printf-style of string formatting with % symbols is really old and ugly. Look up how to do f-strings.

Okay, thank you!
Reply 3
Original post by askleo
Hey, I'm trying to write some Python code, but I've messed up. I wrote this:

name = input("What is your name?")
print("Hello %s! Nice to meet you!" % name)
response = input("How many letters are in your name?")
length = len(name)
if response == length :
print("That's right!")
else:
print("No, the length of your name is %i!" % length)

but when I run it, it does this:

What is your name? leo
Hello leo! Nice to meet you!
How many letters are in your name? 3
No, the length of your name is 3!

Does anybody know where I went wrong?

Ik this has already been replied to but if you change the input(...) to int(...) it should work!! So:

response = int("How many letters are in your name?")

Hope that helps (:
Original post by Isa (:
Ik this has already been replied to but if you change the input(...) to int(...) it should work!! So:

response = int("How many letters are in your name?")

Hope that helps (:

That will throw a ValueError because you're supplying the int() function with an invalid string.

This is probs what you meant:
response = int(input("How many letters are in your name? "))
Reply 5
Original post by Composure
That will throw a ValueError because you're supplying the int() function with an invalid string.

This is probs what you meant:
response = int(input("How many letters are in your name? "))

ah gosh yeah sorry my bad that was what I meant T-T,, thank you for correcting me!! (:

Quick Reply

Latest

Trending

Trending