您的老师可能希望您找到 .is_integer() 方法:>>>2.0 .is_integer()真的>>>2.1 .is_integer()错误的另请参阅如何检查浮点值是否为整数.要将 '2.0' 转换为浮点数,可以调用 float() 函数:#!/usr/bin/env python3为真:尝试:f = float(input('请输入一个数字:'))除了值错误:print('再试一次')别的:break # 得到一个数字这是在 Python 中从 stdin 获取数字的正确方法.如果您的老师禁止使用 ValueError 和 break 那么您可以使用正则表达式:if re.match("^[+-]? *(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$", input_string): 接受 20e-1 == 2.0 == 2.请参阅提取浮点/双精度值.要测试输入是否为空且只包含十进制数字,即检查是否为非负整数,可以使用input_string.isdecimal()方法如果调用 int(input_string) 并捕获 ValueError 是被禁止的.请参阅如何在 Python 中检查字符串是否为数字?.I am trying to make it that the user will get an error message back if they enter alphabetical letters or non-integer numbers, and when an integer is entered the program will proceed to show this integer number being squared and cubed. My teacher doesn't want any 'breaks' in code or any ValueErrors.print("Squaring and cubing integer program has started") #Introduction of program for userUserNumber=input("Enter an integer to be raised to the power of 2 and 3: ") #Asks user to input an integer, that will then be raised to the power of 2 and 3while '.' in UserNumber or UserNumber.isalpha(): #Error-trap to test if input contained letters of the alphabet or contains decimal point print("You have not entered an integer. Try again and enter an integer!") #Alert for user to inform that their entry was invalid (not an integer) UserNumber=input("Enter an integer to be raised to the power of 2 and 3: ") #Asks user again to enter an integer, if they did not previously.print(int(UserNumber), " is the integer you entered.") #Displays to user their inital integerprint(int(UserNumber), " squared is ", int(UserNumber)**2) #Displays to user their input integer squaredprint(int(UserNumber), " cubed is ", int(UserNumber) **3 ) #Displays to user their input integer cubedprint("Calculations are now finished.") #Output to show program has ended 解决方案 2.0 is not an integer; it is a float. float in Python is similar to the standard IEEE 754 Floating Point (if platform supports IEEE 754 Floating Point then Python probably uses it) that have limited precision. Integers in Python have infinite precision (until you run out of memory):>>> i = 123456789012345678901234567890>>> i == 123456789012345678901234567890True>>> f = i * 1.0 # make it a float>>> f.is_integer() # check whether it is a whole numberTrue>>> f == 123456789012345678901234567890FalseNever mix float and int unless you know that it won't change the result in your case e.g., 2 == 2.0 but as the example shows it can be false for larger numbers.Your teacher probably expects that you find .is_integer() method:>>> 2.0 .is_integer()True>>> 2.1 .is_integer()FalseSee also, How to check if a float value is a whole number.To convert '2.0' to a float, you could call float() function:#!/usr/bin/env python3while True: try: f = float(input('Enter a number: ')) except ValueError: print('Try again') else: break # got a numberIt is the correct way to get a number from stdin in Python. If your teacher forbids using ValueError and break then you could use a regular expression:if re.match("^[+-]? *(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$", input_string): that accepts 20e-1 == 2.0 == 2. See Extract float/double value.To test whether the input is not empty and contains only decimal digits i.e., to check whether it is a nonnegative integer, you could use input_string.isdecimal() method if calling int(input_string) and catching ValueError is forbidden. See How do I check if a string is a number in Python?. 这篇关于如何接受像“2.0"这样的整数但例如“2.1"作为浮点数?Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 16:03