If/else statement - print a certainly string depending on input from user


#1

https://www.codecademy.com/courses/learn-python/lessons/pyglatin/exercises/check-yourself-some-more?action=resume_content_item


Hi,

I’m trying to code Python Pig Translator. I’m just in the beginning where I have to declare an if/else statement to check length of the input string (string > 0 chars.) and to check of the string only contains letters by using .isalpha().

Depending on what the user typed as input I want to print two different string.
If the user type “11” which is non-letters chars. I want to print “Please type only letters.”
If the user type “” (non chars.) then I wont to print “Please type on some letters.”

I have tried to get this working but I couldn’t. How can I solve this problem? :slight_smile:


#2

this one is easy, you could check if the length equals zero or original equals an empty string.

what about spaces? What if the user enters: "23 45", what message should be displayed then? Or what about: "hello 24"?


#3

If you want to do what you’ve said, I would create a first if statement that checks if it is empty and prints what you want it to print. Then use an elif statement to check if it isn’t solely constituted of letters and prints out what you want printed if that’s the case and then finally use an else statement that does something if everything went well. If you don’t know what an elif statement is, it checks if some condition is true if the previous if and elifs didn’t execute.

if 3 == 4:
    print("3 is 4!")
elif 4 + 5 != 9:
    print("3 is not 4 and 4 + 5 is not 9!")
elif len([4, 3, 2]) = 2:
    print("3 is not 4 and 4 + 5 is 9 and [4, 3, 2] has a length of 2.")
elif 3 ** 2 == 9:
    print("3 is not 4 and 4 + 5 is 9 and [4, 3, 2] doesn't have a length of 2 and 3 squared is 9.")
elif 2 - 1 == 1:
    print("3 is not 4 and 4 + 5 is 9 and [4, 3, 2] doesn't have a length of 2 and 3 squared is not 9 and 2 - 1 is 1.")
else:
    print("Nothing is true here.")

Here only the elif statement that checks if 3 ** 2 == 9 is executed because all other checks before it didn’t execute(because their conditions were false). The elif after it isn’t checked because an if orelif before it was already executed.


#4

Hi, I have tried what you mentioned but I can’t make it work.

Here is my code - will you take a look at it at tell me what I’ve done wrong?

original = raw_input(“Enter a word:”)
if len(original) > 0:
print original
elif original.isalpha():
print original
else:
print “Please type some letters”

The if statement will always be true and executed because words of non-letter and letter always will be >0, so the elif statement will never be executed. How can I bypass that? :slight_smile:


#5

I have fixed the first problem with non chars. but I haven’t figured out how to solve non-letter words with whitespace and word mixed of letter and non-letters.

My source code:

original = raw_input(“Enter a word:”)
if len(original) > 0:
print original
elif original.isalpha():
print original
else:
print “Please type some letters”

How can I improve it?


#6

This is why lesson went for valid vs invalid input (if/else)

you didn’t answer my question, if the user enters "hello 24", what messages should be displayed? I can’t help you solve something if you don’t know what you want to make it look like


#7

Sorry, my bad.

If the user type in "hello 24" I want the output to be “Please type only letters - no non-chars.”


#8

then its simple, add another else if to check if the string isn’t empty, i already mentioned this:

so then we have:

valid input (length greater then zero and isalpha
empty input
invalid input (spaces, numbers and so on)


#9

Okay. I actually thought that I have checked if the length > 0.

if len(original) > 0:
  print original

I will try with a else if and see if I can get this working properly :slight_smile:


#10

Would you put len(original) and isalpha() in the same if statement by using and?


#11

You could go for:

if length > 0:
   if isalpha():
      print original
   else:
      print "Please type only letters - no non-chars."
else:
   print "empty"

or something like that. The above is not actual code, just an indication of design in pseudo code.


#12

Awesome. I will try to work something out :slight_smile:


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.