仅使用try/except设置输入范围

2024-06-15 09:15:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个非常新的编码,似乎有我的工作,但我需要它拿起超过10的值作为一个错误。我不知道如何在不使用条件结构(if etc)或重复(for, while)的情况下实现它

while True:
 try:
    value = int(input("Enter an integer between 1 and 10: "))
    reciprocal = 1 / value
    print("The Reciprocal of your number is ", reciprocal, "\n")

except ValueError:
    print("You did not enter an integer!!!\nPlease, try again.\n")
except ZeroDivisionError:
    print("Oops, you entered zero.\nPlease, try again. \n")

Tags: an编码ifvalue错误integer条件结构