pythonloopswhile-loop

How to get next user input in python for while loop?


I am currently in school taking a python course in this lab it is asking me to create a loop that outputs the users input into a sentence until user enters quit

Ex: If the input is:

apples 5
shoes 2
quit 0

the output is:

Eating 5 apples a day keeps the doctor away.
Eating 2 shoes a day keeps the doctor away.

my code is:

your_input = input() #input string and int 
string_value = your_input.split()
str_value = string_value[0]
int_value = string_value[1]

while 'quit' not in your_input:
    print("Eating {} {} a day keeps the doctor away.".format(int_value,str_value))
    your_input = input()
    break

Output:

Eating 5 apples a day keeps the doctor away.

when it should be:

Eating 5 apples a day keeps the doctor away.
Eating 2 shoes a day keeps the doctor away.

using the input:

apples 5
shoes 2
quit 0

Solution

  • try something like this,

    your_input = input() #input string and int 
    string_value = your_input.split() 
    str_value = string_value[0] 
    int_value = string_value[1]
    
    while 'quit' not in your_input:
        print("Eating {} {} a day keeps the doctor away.".format(int_value,str_value))
        your_input = input()
        string_value = your_input.split() 
        str_value = string_value[0] 
        int_value = string_value[1]
    

    the issue with your current code is that you don't need break as you already have a terminating condition in while statement, then you need to split your input in every iteration of loop