【问题标题】:How to repeat or stop [duplicate]如何重复或停止[重复]
【发布时间】:2023-03-12 10:55:01
【问题描述】:

我正在尝试制作一个询问文本的程序,然后根据需要重复 x 多次。我希望它在完成较早的文本时总是询问您,如果您键入“停止”,它将结束程序。到目前为止,我已经这样做了:

text = input("Give a text: ")
times = int(input("Give a number: "))


for i in range(0, times):
        print(text)

一个例子:

Give a text: Wadap
Give a number: 3
Wadap 
Wadap
Wadap

Give a text: Hi
Give a number: 2
Hi
Hi

Give a text: Stop
Stopping.

【问题讨论】:

  • while 循环是你要找的
  • @Prune 我认为这个问题与您标记为重复的问题无关。不过,OP 的措辞相当混乱。
  • @GiorgosMyrianthous:原理是一样的:循环整个程序,同时/直到用户给出特定的输入。

标签: python repeat


【解决方案1】:

您需要将现有代码放入while 循环中:

text = input("Give a text: ")
while text != "Stop": 
    times = int(input("Give a number: "))
    for i in range(0, times):
        print(text)
    text = input("Give a text: ")
print("Stopping")

【讨论】:

    【解决方案2】:

    您希望将所有内容都包装在 while 循环中。

    some_condition = True
    while some_condition:
        <your code>
    

    这里some_condition只是一个你可以控制退出程序的变量。

    【讨论】:

    • 我无法修复您手机上的缩进,但为什么您的代码中有python
    • 哎呀,只是我的格式错误
    猜你喜欢
    • 2010-12-22
    • 2017-01-11
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多