Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if-elif-else coding help
#1
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.
Reply
#2
Please show us the if statement you tried, and the full text of the error you are getting.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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'
Reply
#6
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)
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python coding mutiple elif's metro17 1 1,782 Aug-02-2019, 10:27 AM
Last Post: metulburr
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,254 Jul-28-2019, 05:52 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020