試圖學習Python。在這個幾個小時。阻礙練習5.2
我想要做的是輸入幾個數字(例如7,4,6,10,& 2),然後打印最大值和最小值。 我的代碼完美適用於我輸入的每個數字1-9。一旦我達到10或更高,它就會變得不穩定。 它看起來是10讀爲1,零附着它,它說10是最小值。 我在哪裏錯了? 。 練習是輸入幾個數字,然後打印最大值和最小值。我的代碼完美適用於我輸入的每個數字1-9。一旦我達到10或更高,它會變得不穩定,並列出10個最低限度。 我在哪裏錯了? 。
largest = None
smallest = None
the_list = []
while True:
num = input('Enter a number or done: ')
#Handle the edge cases
if num == 'done' : break
if len(num) < 1 : break # Check for empty line
# Do the work
try :
number = int(num)
the_list.append(num)
except:
print("Invalid input")
#continue
print(the_list) # NOTE: This is new so I can see what is in the_list
for value in the_list:
if smallest is None:
smallest = value
elif value < smallest:
smallest = value
for the_num in the_list:
if largest is None:
largest = value
elif the_num > largest:
largest = value #the_num
print("Maximum is", largest)
print("Minimum is", smallest)
'min'和'max'功能存在的原因,btw :) –
是的,謝謝你迴應亞當。我會在不同情況下記住這一點。這項任務比我分享的要多,所以在這種情況下我不能那麼簡單。 –