简体   繁体   中英

python help! If/else statement

I have been learning python through code academy. It asked me to create a if else statement that prints the users input, but if there is no input print "empty". I did pass the tutorial but when there is a user input it prints both the users input and "empty".

here is my code:

print "Welcome to the English to Pig Latin translator!"

original = raw_input("what is your name?")
length = len("original")

if length > 0:
    print original

else: length < 0
print "empty"

Notice that print under else is not indented. I thought you had to indented it, but when i do it gives me an error.

You seem to have a couple issues with your code. First I believe you should change your assignment of length to:

length = len(original)

That way you get the correct length of the string you binded to the name original . If you use len("original") , you will pass the string "original" to the len() function, and it will just count the characters in that string. Regardless of what the user of your program inputted, your length will always be 8, since that's how many characters are in the string "original"

Also keep in mind that the len() function will never return a negative value. So testing if length < 0 doesn't make sense. You probably wanted to test if it equals 0. It seems as if you were trying to use an elif statement.

Try:

print "Welcome to the English to Pig Latin translator!"

original = raw_input("what is your name?")
length = len(original)

if length > 0:
    print original

elif length == 0:
    print "empty"

elif statements let you test conditions just like if statements. elif is short for "else if".

Furthermore, as @chepner pointed out in the comments, there is no need to even test the second condition at all. You can just as easily go with:

else:
    print "empty"

The else syntax is not followed by any condition. It automatically enters the indented block of code if the other conditions evaluate to False.

Was the length < 0 intended to be a comment? You need the comment character # .

else: # length < 0
    print "empty"

It's wrong anyway, it should be length <= 0 .

Without a comment character it was being interpreted as the statement to use as the else clause. It didn't create an error since length < 0 just generates a boolean result that you didn't do anything with, but it prevented you from having another statement as part of the else .

else: length < 0
print "empty"

should be

elif length == 0:
   print "empty"

Python has significant whitespace, things that are indented the same are in the same scope.

There is aa statement after else.

else: length < 0
print "empty"

Maybe you are looking for an ( elif is another way to check another condition after the if if the previous if fails.)

elif length <= 0:

or just a plain

else:
   print "empty"

it will never go past zero anyways you could have a == conditional for zero and it would work.

elif length == 0:

this is probably the best way there is no need to check another condition.

if length > 0:
   print original
else: 
   print "empty"

also just a side note length = len("original")

there is not suppose to be quotation marks around original because its a variable :). You will just be getting the string "original" not the actual stuff inside of the variable.

The end result ..

print "Welcome to the English to Pig Latin translator!"

original = raw_input("what is your name?")
length = len(original)

if length > 0:
    print original
else: 
    print "empty"

First off, it is not len("original") it is len(original) . If you use quotes you are making it a constant value, "original", rather than a variable named original.

Second, instead of checking the length of the string you should use this

if original:
    # Type here what happens if the string is not empty
else:
    # Here what happens if the string is empty

By Python standard any collection, including strings, equals false if it is empty(aka contains no elements) and true if it contains any elements.

To check if there is an item in a list, simply do this:

if List_name:
    {code here}
    ...

So, your code should very simply look like this:

print "Welcome to the English to Pig Latin translator!"
original = raw_input("what is your name?")
if original:
    print original
else:
    print "Empty"

It's that easy :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM