这是我的计算机课程中的Python挑战。我的老师无法弄清楚答案,而我是班上最远的,所以没人知道该怎么做。我已经尝试了所有我能想到的。
Write a program that checks that the value entered is a floating point number.
Check the program works by entering an integer to see if an error is raised.
If a float is entered it should output the number.
这必须在Python 3.4.3中完成
num=input("Enter a number")
if num = float:
print("This is a float")
else:
print("This is not a float")
我不知道如何做到这一点,所以输入不是立即设置为浮点数。
您可以通过一些异常处理此问题,具体取决于转换失败会引发以下事实ValueError
:
input_value = input("> ")
try:
int_value = int(input_value)
print("invalid input: value was an int!")
except ValueError:
try:
float_value = float(input_value)
print(float_value) # just echo
except ValueError as e:
print("invalid input: ", " ".join(e.args))
(内部Python 3)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句