# Help with Computing Homework?

#1
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:

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?
0
3 months ago
#2
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.
0
#3
(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!
0
3 months ago
#4
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:

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 (:
0
3 months ago
#5
(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? "))
0
3 months ago
#6
(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!! (:
0
X

new posts
Back
to top
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

### Poll

Join the discussion

#### Y13's - If you haven't confirmed your firm and insurance choices yet, why is that?

I am waiting until the deadline in case anything in my life changes (5)
20.83%
I am waiting until the deadline in case something else changes (e.g. exams/pandemic related concerns) (1)
4.17%
I am waiting until I can see the unis in person (4)
16.67%
I still have more questions before I make my decision (2)
8.33%
No reason, just haven't entered it yet (7)
29.17%
Something else (let us know in the thread!) (5)
20.83%