3 回答

TA貢獻1798條經驗 獲得超7個贊
在Python 3.x 中,使用input()代替raw_input()
創建一個list將用戶輸入附加到其中,以便稍后對其進行排序。
input_List = []
input_List.append(int(input("Enter a number: ")))
while True:
b = input("Do you want to input more: ")
if b == 'yes':
input_List.append(int(input("Enter a number: ")))
elif b == 'no':
input_List.sort(reverse=True)
break
print(input_List)
輸出:
Enter a number: 5
Do you want to input more: yes
Enter a number: 7
Do you want to input more: yes
Enter a number: 90
Do you want to input more: no
[90, 7, 5]

TA貢獻1936條經驗 獲得超7個贊
它應該是這樣的:
a = input("Enter a number: ")
l = [int(a)]
while True:
b = input("Do you want to input more: ")
if b == 'yes':
c = input("Enter another number:")
l.append(int(c))
elif b == 'no':
l.sort(reverse=True)
print(l)
break

TA貢獻1827條經驗 獲得超8個贊
很多邏輯錯誤,還有更微妙的錯誤
raw_input
返回一個字符串,除非您轉換為整數,否則您將無法獲得正確的數字排序。是/否問題邏輯被打破,來得太晚了......
你永遠不會
append
在你的列表中使用,它總是包含 2 個元素。my_list = [a,c]
沒有意義。
我的建議:
# this is for retrocompatibility. Python 3 users will ditch raw_input altogether
try:
raw_input
except NameError:
raw_input = input # for python 3 users :)
# initialize the list with the first element
my_list = [int(raw_input("Enter a number: "))]
while True:
b = raw_input("Do you want to input more: ")
if b == 'yes':
# add one element
my_list.append(int(raw_input("Enter another number:")))
elif b == 'no':
my_list.sort(reverse=True)
print(my_list)
break
添加回答
舉報