Needs to be in PYTHON 7.6 Warm up: Parsing strings (Python 3) (1) Prompt the use
ID: 3780124 • Letter: N
Question
Needs to be in PYTHON
7.6 Warm up: Parsing strings (Python 3)
(1) Prompt the user for a string that contains two strings separated by a comma. (1 pt)
Examples of strings that can be accepted:
Jill, Allen
Jill , Allen
Jill,Allen
Ex:
(2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts)
Ex:
(3) Using string splitting, extract the two words from the input string and then remove any spaces. Output the two words. (2 pts)
Ex:
(4) Using a loop, extend the program to handle multiple lines of input. Continue until the user enters q to quit. (2 pts)
Ex:
Explanation / Answer
while True:
while True:
s = raw_input("Enter input string:")
if(s=='q'):
break
try:
k = s.index(",")
break
except:
continue
if(s=='q'):
break
s = s.split(",")
print("First word:"+s[0])
print("Second word:"+s[1])
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.