我正在尝试解决以下问题:
编写一个程序,从用户那里输入 3 个整数,然后按升序对这些数字进行排序。完成排序后,程序会询问用户是否要执行其他排序。答案将是"是"或"否"。
程序所需输出的示例是:
- 输入三个整数:30、4、1
- 升序为 1、4、30
- 是否要进行其他排序(是/否(:是
- 输入三个整数:30、4、8
- 升序为 4、8、30
- 是否要进行其他排序(是/否(:是
- 输入三个整数:3、16、7
- 升序为 3、7、16
- 是否要进行其他排序(是/否(:否
- 程序已完成。
您可以使用while
循环,并用五行解决练习:
continue_prog = True
while continue_prog:
num_list = input("Enter three integers separated by commas: ").replace(' ','').split(',')
print(f"The ascending order is: {sorted(num_list, key=int)}")
# Exits unless the answer is yes/Yes/YES
continue_prog= input("Do you want to do another sorting(yes/no)? ").lower().replace(' ','') == 'yes'
输入为
integers = raw_input().split(',').tolist()
if(integers[-1] == "no"):
exit()
我希望这有效