如何在python中重复
我想重复这两行,直到用户为每个请求输入一个整数。如何在python中重复,python,Python,我想重复这两行,直到用户为每个请求输入一个整数。 谢谢你松散地阅读 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: ")) 但是如果严格地重复这两个语句,直
谢谢你松散地阅读
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: "))
但是如果严格地重复这两个语句,直到它们都得到一个int值
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: ")
一个好的开始是学习。然后,看一看一些教程,当你陷入困境时再报告。在学习该语言的基本基础之前,您不会真正充分利用堆栈溢出。类似于do while loopIt的操作会返回错误,我必须再次重新加载代码。我想重复这两行,直到用户为每个请求输入一个整数。它返回一个错误,因为你告诉代码无论用户给它什么。如果不是数字会怎么样?Python将抛出一个错误。阅读语句以捕获错误并继续执行
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: ")
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