找到1到10之间整数的倒数
因此,我需要编写一个程序,该程序请求一个从1到10的整数并计算其倒数。
while True:
try:
integer = int(input("Enter an integer between 1 to 10 "))
parameter = integer in range(1,10)
equation = 1/integer
print(equation)
break
except ValueError:
print("not int")
so I need to write a program that requests an integer from 1 through 10 and calculates its reciprocal.
while True:
try:
integer = int(input("Enter an integer between 1 to 10 "))
parameter = integer in range(1,10)
equation = 1/integer
print(equation)
break
except ValueError:
print("not int")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
assert> assert
检查数字是否在1和10范围内。此外,我认为,您不应该使用break
,因为您想永远获得整数。 :输出:
You can use
assert
to check if the number is in the range of 1 and 10. Also, in my opinion, you shouldn't be usingbreak
because you want to get the integer forever. :Output:
假设OP不考虑尝试/除了违反不使用任何“条件结构”的要求:
Assuming OP doesn't consider try/except to contravene the requirement of not using any "conditional structure" then:
由于布尔值是整数的事实:
因此您的问题可以如下解决:
By virtue of the fact that bool value is integer:
So your problem can be solved as follows:
你真的很近!
唯一要更改的是如何格式化输出,请尝试这样的事情:
You are really close!
The only thing to change is how the output is formatted, try something like this: