如何在python中重复

时间:2014-03-05 00:41:19

标签: python

sides=int(input("Please enter the amount of sides on the dice: "))
times=int(input("Please enter the number of times you want to repeat: "))

我想重复这两行,直到用户为每个请求输入一个int号 感谢

2 个答案:

答案 0 :(得分:2)

松散地阅读,

def get_int(prompt):
    while True:
        try:
            return int(input(prompt))
        except ValueError:    # wasn't an int
            pass

sides = get_int("Please enter the amount of sides on the dice: ")
times = get_int("Please enter the number of times you want to repeat: ")

但如果您严格要重复两个语句,直到两者获得一个int值,

while True:
    s1 = input("Please enter the amount of sides on the dice: ")
    s2 = input("Please enter the number of times you want to repeat: ")
    try:
        sides = int(s1)
        times = int(s2)
        break
    except ValueError:
        pass

答案 1 :(得分:0)

sides=0;times=0
while(sides == 0 and times == 0):
    sides=int(input("Please enter the amount of sides on the dice: "))
    times=int(input("Please enter the number of times you want to repeat: "))