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条经验 获得超6个赞
它应该是这样的:
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
添加回答
举报