Python Forum

Full Version: if-elif-else coding help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am trying to write a Python program to do the following:

a. Ask the user to enter a str containing at least 6 characters. Assign the str entered to a variable mystr.
b. If mystr has less than 6 characters print ‘<mystr > is not a valid str.’
c. Otherwise, if mystr consists of only alphabetic characters print ‘You have entered a valid alphabetic string.’
d. Otherwise, do the following:
i. print a statement: ‘<myint> is a valid non-alphabetic str.’
ii. Find the index of the first occurrence of ‘e’ in mystr and print this value with a suitable message. A value of -1 indicates mystr does not contain ‘e’.

I have started with:
mystr = input('Enter a str containing at least 6 characters: ')

However, I am unable to figure out the if statement that follows this that counts the number of characters and if it is less than 6, to print the respective message. I am getting a syntax.
Please show us the if statement you tried, and the full text of the error you are getting.
The if statement I tried was:

If len(mystr) < 6
print(‘<mystr> is not a valid str’)
else:
print(‘<mystr> is valid’)

Based on the program I need to write, I understand the else statement should not be written there yet however, I just wanted to test if these if and else statements work. I am getting a “Syntax” error next to the “...< 6” line.
Python is case-sensitive. 'If' should be 'if'. Also, please use python and output tags when posting code and results. See the BBCode link in my signature below for instructions.
In addition, there should be a colon at the end of the if statement: if len(mystr) < 6:.
Furthermore, your quote characters are not actually quote characters. They're some sort of weird multi-byte horror, similar to the trash Microsoft Word frequently replaces quotes for. To demonstrate, here's a single quote character x, and the two different make-believe nonsense characters you had (they are NOT the same, and it DOES matter):
>>> x = "'"
>>> x.encode()
b"'"
>>> y = "‘"
>>> y.encode()
b'\xe2\x80\x98'
>>> z = '’'
>>> z.encode()
b'\xe2\x80\x99'
My apologies, I was actually replying with my phone and so could not use python tags.
The colon at the end of the if statement was the issue I was having (I did have if in lowercase in the actual program). Also, the quotations seem to be working okay as well.

In part c, how do I show that he str only has alphabetical characters and
in part d.i. where does myint come from? How do I include this into the program?

(I am a beginner in Python)
(Oct-29-2018, 10:18 PM)esyfilamaj Wrote: [ -> ]d. Otherwise, do the following:
i. print a statement: ‘<myint> is a valid non-alphabetic str.’

Based on the context, I believe that's a typo, and should be mystr, to match with the variable you previously created. It makes sense with what came before, and what follows afterward. Plus, saying "some-int is valid non-alphabetic str" is weird, and doesn't make sense.