Home > Net >  How to get the length of a value from user input?
How to get the length of a value from user input?

Time:12-28

I'm working on an assignment:

My input is supposed to be a name, it could be anything.
The output is supposed to be the length of the name.

e.g.

Currently, I have:

print(input("What is your name? ")

print(len(input)

But the second half isn't correct.

CodePudding user response:

If you need to DO something with the value, then you have to store it in a variable:

name = input("What is your name? ")
print(name)
print(len(name))

CodePudding user response:

I think I located the problems in your code. This is what it should look like!

n = input("What is your name? ")

print(str(len(n)))

I tested and switched things up a bit by adding a variable called "n" and giving it the input value. Then I wrote a changed into integer version of the len of n... If that makes sense.

I put three brackets in the second line because the first one is for the print statement, the second one len, and the last one for the int. Don't worry too much as it was an honest mistake! :)

PS. This is the better version of the code:

n = input("What is your name? ")

print("Your name has " str(len(n)) " letters")
  • Related