如何在python for while循环中获取下一个用户输入?

问题描述 投票:0回答:1

我目前正在学校学习本实验室的Python课程,它要求我创建一个循环,将用户输入输出到句子中,直到用户输入退出

例如:如果输入是:

apples 5
shoes 2
quit 0

输出为:

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

我的代码是:

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

输出:

Eating 5 apples a day keeps the doctor away.

当它应该是:

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

使用输入:

apples 5
shoes 2
quit 0
python loops while-loop
1个回答
0
投票

尝试这样的事情,

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]

当前代码的问题是您不需要中断,因为 while 语句中已经有终止条件,那么您需要在循环的每次迭代中吐出输入

© www.soinside.com 2019 - 2024. All rights reserved.