![](/img/trans.png)
[英]How can I make an if statement that recognizes whether or not my input appears in a list?
[英]How can I accept the user input in such a way that my program recognizes whether it is an int or a float?
谁能告诉我如何接受 integer 并为我的输入浮动(在列表中)? 因为如果我输入完整的数字,结果总是 a.0
这是我的代码:
user = int(input("How many numbers do you want to sum up: "))
list = [float(input("Number: ")) for i in range (user)]
print("The Sum of the entered Numbers is: ", sum(list))
我的 output 看起来像这样:
How many numbers do you want to sum up: 3
Number: 2
Number: 1
Number: 5
The Sum of the entered Numbers is: 8.0
你可以做:
user = int(input("How many numbers do you want to sum up: "))
list = [float(input("Number: ")) for i in range (user)]
x = sum(list)
if x.is_integer():
x = int(x)
print("The Sum of the entered Numbers is: ", x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.