Help with Computing Homework?

Watch this thread
askleo
Badges: 2
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#1
Report Thread starter 3 months ago
#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:

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?
0
reply
Composure
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#2
Report 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
reply
askleo
Badges: 2
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#3
Report Thread starter 3 months ago
#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
reply
Isa (:
Badges: 3
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#4
Report 3 months ago
#4
(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 (:
0
reply
Composure
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#5
Report 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
reply
Isa (:
Badges: 3
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#6
Report 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
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

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

Personalise

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%

Watched Threads

View All