當我嘗試運行此代碼時,出現不正確的最大值和最小值。有誰能告訴我如何解決它?我不允許使用'max'和'min'。Python代碼中的最大值和最小值不正確
更新:我更新了代碼,它仍然無法正常工作。 更新2:代碼現在可用!十分感謝大家!
minimum=float('inf')
maximum=None
count=0
total=0
number=input ("Please enter the amount of numbers you wish to categorize: ")
while True:
num = input("Enter a number: ")
count+=1
total+=num
if num is None or num < minimum:
minimum = num
if num is None or num > maximum:
maximum = num
if count == number:
break
print "The average of your numbers is ", round ((total/count),2),"."
print 'The largest number is:', maximum,"."
print 'The smallest number is:', minimum,"."
這是功課嗎? – hochl 2012-03-26 01:50:33
我假設這是作業,正如你所說的:「我不允許使用'max'和'min'」。將來,請將「家庭作業」標籤添加到任何作業問題中。 – agf 2012-03-26 01:51:22
是的,抱歉。 – user1292009 2012-03-26 01:52:58