Question

6.6 Warm up: Parsing strings (Python 3) (1) Prompt the user for a string that contains...

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 It has to be on python 3, the answer here give an error thank you.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:


# while loop to take input until user want to exit
while True:
# taking input from the user
userString = input("Ehter input String: ")
if userString == "q":
exit(0)
# spliting the input by comma
strings = userString.split(',')
# checking whether comma is present in the string or not
if ',' not in userString:
print("Error: No comma in string")
print("Try Again..")
continue
# checking user has entered exactly two string or not
if len(strings) > 2:
print("More then two strings are entered.")
print("Try Again..")
continue
# printing the words
print("First word: " + strings[0].strip())
print("Second word: " + strings[1].strip())
  

Code:

Sample Output:

Note: Let me know if you have any doubt.

Add a comment
Know the answer?
Add Answer to:
6.6 Warm up: Parsing strings (Python 3) (1) Prompt the user for a string that contains...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 9.13 LAB: Warm up: Parsing strings (1) Prompt the user for a string that contains two strings separated by a comm...

    Python 9.13 LAB: Warm up: Parsing strings (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...

  • Warm up: Parsing strings

    5.9 LAB: Warm up: Parsing strings(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, AllenJill , AllenJill,AllenEx: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) Extract the two words from the input string...

  • This code is not working while True: names = input("Enter input string:") if names == "q":...

    This code is not working while True: names = input("Enter input string:") if names == "q": break validlnput ="true" while "," not in names: validlnput="true" print ("Error: No comma in string.") names = input("Enter input string:") # Check for q to terminate if names == "q": exit(0) #set the flag else: validlnput="false" s = names.split(",") print ("First word:",s[0].strip()) print ("Second word:",s[1].strip()) print ("\n") The output should read Enter input string: Jill, Allen First word: Jill Second word: Allen Enter input...

  • Code is wrong: python parsing warm up. while(True): inp_str = input('Enter input string: ') if (inp_str...

    Code is wrong: python parsing warm up. while(True): inp_str = input('Enter input string: ') if (inp_str == 'q'): break; while inp_str.find(",")==-1: print('Error: No comma in string. ') inp_str=input('Enter input string: ')       s=inp_str.split(",") print('First word:'+ s[0].strip()) print('Second word:'+ s[1].strip()) print('\n') This is my code for a problem but the output is wrong and I dont know how to fix it (the top is the output im getting the bottom is what i am supposed to get) Enter input string:...

  • C++ (1) Prompt the user to enter a string of their choosing (Hint: you will need...

    C++ (1) Prompt the user to enter a string of their choosing (Hint: you will need to call the getline() function to read a string consisting of white spaces.) Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!...

  • C++ please! (1) Prompt the user to enter the name of the input file. The file...

    C++ please! (1) Prompt the user to enter the name of the input file. The file will contain a text on a single line. Store the text in a string. Output the string. Ex: Enter file name: input1.txt File content: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! (2) Implement a PrintMenu() function,...

  • Write a Python script in that will prompt the user to enter a string. After the...

    Write a Python script in that will prompt the user to enter a string. After the user enters their string print its length and the ordinal value of the first, third, and seventh character. Assume the input will be of sufficient length. Use "nice" input and output

  • Prompt the user to enter two character strings. The program will then create a new string...

    Prompt the user to enter two character strings. The program will then create a new string which has the string that appears second, alphabetically, appended to the end of the string that shows up first alphabetically. Use a dash (-') to separate the two strings. You must implement the function string appendStrings(string, string), where the return value is the combined string. Display the newly created string. Your program must be able to handle both uppercase and lowercase characters. You are...

  • **C programming Language 3) Prompt the user for data points. Data points must be in this...

    **C programming Language 3) Prompt the user for data points. Data points must be in this format: string, int. Store the information before the comma into a string variable and the information after the comma into an integer. The user will enter -1 when they have finished entering data points. Output the data points. Store the string components of the data points in an array of strings. Store the integer components of the data points in an array of integers....

  • Write a program that uses String method regionMatches to compare two strings input by the user....

    Write a program that uses String method regionMatches to compare two strings input by the user. The program should prompt the user to enter two strings, the starting index in the first string, the starting index in the second string, and the number of characters to be compared. The program should print whether or not the strings are equal, (Ignore the case of the characters during comparison.) 四SAMPLE RUN #1: java StringCompare Highlight: None D Show Highlighted Only Interactive Session...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT