我无法让我的程序停止重复我的第一条语句

时间:2019-06-25 20:15:35

标签: python loops random

Python只是不断地重复我一次:“您想要多少个随机数?并且不会继续进行下一个输入。

我已经尝试了大多数我能想到的东西。

do_program = True
while(do_program):
while(True):
    try:
        number_of_numbers = float(input("How many random numbers do you want?"))
        if(number_of_numbers < 0):
            print("Negative numbers are not allowed.")
        continue
    except ValueError:
        print("The value you entered is invalid. Please enter numerial values only.")
    else:
        break

while(True):
    try:
        lowest_number = float(input("What is the lowest random number you want?"))
        if(lowest_number < 0):
            print("Negative numbers are not allowed.")
        continue
    except ValueError:
        print("The value you entered is invalid. Please enter numerial values only.")
    else:
        break

while(True):
    try:
        highest_number = float(input("What is the highest random number you want?"))
        if(highest < 0):
            print("Negative numbers are not allowed.")
        continue
    except ValueError:
        print("The value you entered is invalid. Please enter numerial values only.")
    else:
        break
import random
print("The numbers were written to randomnum.txt.")

def main():
    for count in range(number_of_numbers):
        number = random.randint(lowest_number, highest_number)
        print(number)
main()

现在,我只想专注于进入第二和第三输入语句。

1 个答案:

答案 0 :(得分:0)

成功读取number_of_numbers后,代码break从内部while(True)循环中退出,然后恢复外部while(do_program)循环。这将导致反复执行相同的代码。也许在返回主循环之前重新处理您的代码以处理输入?