有没有办法让一个程序在python中运行一次以上[重复]。

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

我有这个代码,只运行一次。谁能帮我把这段特殊的代码循环起来无限期地运行呢,先谢谢了。

这是我的代码。

    def get_non_negative_int(prompt):
        while True:
            try:
                value = int(input(prompt))
            except ValueError:
                print("Sorry, I didn't understand that.")
                continue

            if value < 0:
                print("Sorry, your response must not be negative.")
                continue
            else:
                break
        return value

    age = get_non_negative_int("Please enter your age: ")

    kids = get_non_negative_int("Please enter the number of children you have: ")
python
1个回答
1
投票

如果你要求你的程序在输入正值后再次加载,那么你只需要在程序中添加一个 while 循环。

while(True):
  age = get_non_negative_int("Please enter your age: ")
  kids = get_non_negative_int("Please enter the number of children you have: ")

这将无限期地运行。

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