Python 检查偶数是否因类型问题而失败
我在一个低级编码类,我必须创建一个程序,用户在其中输入一个整数。然后,程序运行一系列循环,以确定用户输入的序列的最小值和最大值,以及用户输入的偶数和奇数的计数,同时还保留已输入的所有整数的运行总数。输入的空行告诉程序停止并输出前面所述的信息。正如问题所述,我不确定在程序进入循环并运行一个周期后如何修复错误。以下是迄今为止的代码:Python 检查偶数是否因类型问题而失败,python,Python,我在一个低级编码类,我必须创建一个程序,用户在其中输入一个整数。然后,程序运行一系列循环,以确定用户输入的序列的最小值和最大值,以及用户输入的偶数和奇数的计数,同时还保留已输入的所有整数的运行总数。输入的空行告诉程序停止并输出前面所述的信息。正如问题所述,我不确定在程序进入循环并运行一个周期后如何修复错误。以下是迄今为止的代码: counte=0 counto=0 sum=0 num=(input("Enter an integer (blank line to quit):")) max=0
counte=0
counto=0
sum=0
num=(input("Enter an integer (blank line to quit):"))
max=0
min=0
value=0
if(num==''):
print("No input values were provided.\nQuitting the program!!!")
while (num!=''):
value=int(input("Enter an integer (blank line to quit):"))
if(num%2==0):
counte+=1
else:
counto+=1
sum+=value
if(value>max):
max=value
if(value<min):
min=value
num=int(input("Enter an integer (blank line to quit:"))
print("The smallest value is",min, "and the larges value is",max,".")
print("The user entered",counte,"even, and",counto,"odd values.")
print("The cumulative total is",total,".")
运行程序时出现的语法错误如下所示:
Traceback (most recent call last):
File "*sensitive information*", line 12, in <module>
if(num%2==0):
TypeError: not all arguments converted during string formatting
只需将其转换为实际整数:
num = int(input("Enter an integer (blank line to quit):"))
您得到的是一个类型错误,而不是语法错误。我认为您的意思是值%2==0,而不是num%2==0,因为num是str,而不是int。