1

I'm new to programming and currently in school. I'm having an issue with an assignment where I need to create a loop for multiple inputs until the user enters q to end. Now this question as a whole assignment has been asked before linked here Trying to hammer out this zylabs Lab but struggling and I have been following @Splatmistro list to complete the assignment for the most part. But, I only need help to understand what is missing from my code to work correctly.

input_comma = input('Enter input string: \n')
**if input_comma != 'q':**
    while ',' not in input_comma:
        print('Error: No comma in string.')
        input_comma = input()
        print('Enter input string: ')

split_input = input_comma.split(',')
print('First word:',split_input[0].strip())
print('Second word:',split_input[1].strip())

I know I need an IF statement before the while statement but the one I have doesn't seem to be working. I know I missing something but I just can't figure it out. Any help would greatly be appreciated.

EDITED: This is the intended output of the code im working on.

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

My code will take and print the first input correctly but it will not proceed to loop for a 2nd, 3rd, or 4th input. This is what I was asking for help with.

TheKoaohan
  • 15
  • 5
  • 1
    Fix your indentation. Code under while blocks should be a level of indentation higher than the indentation of the while loop above it. – Primusa Apr 07 '18 at 01:49
  • 1
    Sorry I did not have it spaced correctly above I edited it to resemble my code the best I could. This is my first post and I'm still learning how some things work. – TheKoaohan Apr 07 '18 at 01:58
  • 1
    Hi could you elaborate a bit more on what is not working? what is the intended function of your code? – Primusa Apr 07 '18 at 01:58
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). While not answering your specific question, the strategies in that q&a will help you. – wwii Apr 07 '18 at 02:03
  • @Primusa I need the code to list 4 inputs. Three of them are two-word phrases separated by a comma and the 4th is the letter q to end the program. I will edit the post to include the overall result I'm trying to achieve. – TheKoaohan Apr 07 '18 at 02:19
  • @wwii I have looked over "Asking the user for input until they give a valid response" but I'm still not seeing whats missing. It may be that I did fully understand python yet, but I don't have an issue with the actual input just the looping output of multiple responses. I will continue to read through the thread in case I missed something. Thank you – TheKoaohan Apr 07 '18 at 02:35
  • 1
    How about @Jono2906's answer? – wwii Apr 07 '18 at 02:36

2 Answers2

1

Instead of using an if statement, use another while loop, like so:

input_comma = input('Enter input string: \n')
while input_comma != "q":
    while "," not in input_comma:

        print('Error: No comma in string.')
        input_comma = input()
        print('Enter input string: ')

    split_input = input_comma.split(',')
    print('First word:',split_input[0].strip())
    print('Second word:',split_input[1].strip())
    input_comma = input('Enter input string: \n')

Note that I also indented the last three lines...that makes the program work according to the example post you supplied.

wwii
  • 23,232
  • 7
  • 37
  • 77
lyxαl
  • 1,108
  • 1
  • 16
  • 26
0

Fixed Code:

input_comma = input('Enter input string:\n')
while input_comma != "q":
    while "," not in input_comma:

        print('Error: No comma in string.\n')
        input_comma = input()
        print('Enter input string:')

    split_input = input_comma.split(',')
    print('First word:',split_input[0].strip())
    print('Second word:',split_input[1].strip())
    input_comma = input("\n"'Enter input string:\n')
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 16 '23 at 00:39