View the step-by-step solution to:

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...

6.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:

Enter input string: Jill, Allen


(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:

Enter input string: Jill Allen
Error: No comma in string.
Enter input string: Jill, Allen


(3) Using string splitting, extract the two words from the input string and then remove any spaces. Output the two words. (2 pts) 


Ex:

Enter input string: Jill, Allen
First word: Jill
Second word: Allen


(4) Using a loop, extend the program to handle multiple lines of input. Continue until the user enters q to quit. (2 pts) 


Ex:

Enter input string: Jill, Allen
First word: Jill
Second word: Allen


Enter input string: Golden , Monkey
First word: Golden
Second word: Monkey


Enter input string: Washington,DC
First word: Washington
Second word: DC


Enter input string: q


I am working on the above lab for my computer science class (which I feel like I am struggling a lot in) and I have entered the below code but it endlessly loops the first input. I am not sure where I am going wrong and am worried about failing this class...


str = input('Enter your input: ')

if str.find(',') == -1:

  print('nError: no comma in string')

  str = input('Enter your input: ')

   

while str.find(',') != -1:

  name = str.split(',')

  first = name[0].strip()

  last = name[1].strip()

   

  print('nFirst word:', first)

  print('Second word:', last)

  print('n')

   

  if str == 'q': 

    break

Top Answer

The endless moving of the program is done because of the while loop as soon as you enters the first input with the comma,... View the full answer

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online