How can I check the length of the user’s input?

Question

How can I check the length of the user’s input?

Answer

In the example in the instructions, an empty string is assigned to the empty_string variable, and then the length of that is checked, using the len() method.
In our code, we want to check the user’s input, which we stored in a variable called original, so we’d just have to use len() on that and compare against 0.
If you think you’ve done it correctly but aren’t able to pass, be sure you’ve put a colon : after your if and else, and that you’ve properly indented the code inside of each. The editor should automatically indent your code after you type a colon and press enter, letting you know you’ve done it correctly. Check the indentation of the example in the instructions if you need a reminder of how to do this!

1 Like

The check fails when user inputs as the input because string length is greater than 0 but there is no actual word.
Please clarify this bug.

3 Likes

I’m having the same issue, can we get some clarification please?

2 Likes

Here’s my code and I can’f figure out why is not printing. I’m stuck, can you help? Thanks!

print ‘Welcome to the Pig Latin Translator!’

Start coding here!

original = raw_input("Enter a word: ")

def greater_0(original):
if len(original) >0:
return original
else:
return “empty”

print len_greater_0(original)
print len_greater_empty(empty)

I am getting the same “TypeError: object of type ‘builtin_function_or_method’ has no len()” regardless of whether my string has zero or any number of characters in it.

Here is my code:

raw_input("Enter a word: ")
original = raw_input

if len(original) > 0:
print original

else:
print “empty”

2 Likes

1 print ‘Welcome to the Pig Latin Translator!’
2
3 # Start coding here!
4 original = raw_input(“Kate:”)
5 if len(original) > 0:
6 print original
7 else:
8 print “empty”

Gets an error of:

File “python”, line 7
else:
^
SyntaxError: invalid syntax

Can someone tell me what I’m doing wrong?

Thanks

To answer, we must see the code indented as you wrote it. Please use the code icon </> that is near the middle of the menu bar at the top of the text box you are typing in.

Hi, Adam did you solve your question? I hope you did, but for what I can tell the error is that the input the user is typing is not stored in original and you are telling the program to test the length of the original.

To fix it:

original = raw_input("Enter a word: ")

if len(original) > 0:
print original

else:
print “empty”

5 Likes

Hello, why my code doesn’t work?

Can someone explain to me why saving raw_input() on original as line 5 doesn’t work

  print 'Welcome to the Pig Latin Translator!'

# Start coding here!
raw_input('tell me a word')
original=raw_input()

if len(original)>0:
  print original
  else :
  print 'empty'

I believe is because, on line 4 your input doesn’t get saved.

It should be original = raw_input(‘Text here’).

So the data is store in a variable. Makes sense? Maybe is not the proper answer but it is what I understand.

1 Like

Yes, but if you see, I after save after an

original=raw_input()

it should work (?)

raw_input() that meaning type some thing in the screen and everything in double round brackets after raw_input will show on screen as a comment
in line 4 : raw_input(‘tell me a word’) It will show on the screen: tell me a word, then you type something BUT IT NOT SAVE
IN LINE 5 original=raw_input() It will show on the screen: because double round brackets after raw_input is a blank and original=raw_input() is waiting the value you type on screen show it doesn’t work

can anyone please tell me what this error message means

try to indent your function’s code

Please correct your if to this way
if len(original) > 0 and original.isalpha():

Thank you for the original.isalpha(). I was wondering why entering a space as an input gave me no output.

I have the same code and it does not work. any ideas?
print ‘Welcome to the Pig Latin Translator!’

Start coding here!

original = raw_input(‘Enter a word:’)

#check if user typed something

if len(original) > 0 and original.isalpha():

print original

else:

print ‘empty’

I get:

Welcome to the Pig Latin Translator!
Enter a word:
Traceback (most recent call last):
File “python”, line 4, in
ExecTimeoutException: Program took too long to terminate.

I am having the same problem I believe that the platform is not working well

1 Like

If refreshing and other trick doesn’t work, then the fallback is to create a string literal and use that to run the program without getting the solution.

Clicking twice over the blinking cursor in the console, make you able to type the word.

2 Likes