输入三个整数并升序,键入"是"保持程序运行,如果键入"否"结束程序



我正在尝试解决以下问题:

编写一个程序,从用户那里输入 3 个整数,然后按升序对这些数字进行排序。完成排序后,程序会询问用户是否要执行其他排序。答案将是"是"或"否"。

程序所需输出的示例是:

  1. 输入三个整数:30、4、1
  2. 升序为 1、4、30
  3. 是否要进行其他排序(是/否(:是
  4. 输入三个整数:30、4、8 
  5. 升序为 4、8、30
  6. 是否要进行其他排序(是/否(:是
  7. 输入三个整数:3、16、7 
  8. 升序为 3、7、16
  9. 是否要进行其他排序(是/否(:否
  10. 程序已完成。

您可以使用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()

我希望这有效

最新更新