【发布时间】:2020-03-12 08:01:16
【问题描述】:
我需要编写一个代码,让用户输入五个数字并将其从大到小排序。他们还可以选择是否要删除、添加或更改变量以及创建、保存或打开列表。不知道怎么做保存、打开和制作部分,所以我删除了,请帮助我。
l=1
print ("Please input five numbers")
a=int(input("1.:"))
print (a, "is the first number.")
print()
b=int(input("2.:"))
print (b, "is the second number.")
print()
c=int(input("3.:"))
print (c, "is the third number.")
print()
d=int(input("4.:"))
print (d, "is the fourth number.")
print()
e=int(input("5.:"))
print (e, "is the fifth number.")
print()
x=[a, b, c, d, e]
y=sorted(x,reverse=True)
print (y)
while l==1:
print()
print ("If you want to delete a number, press D.")
print()
print ("If you want to add a number, press A.")
print()
print ("If you want to change a number, press C.")
print ()
print ("If you want to exit, press Q.")
print ()
z=input("Answer: ")
if z=="A":
f=int(input("Please input another number: "))
x.append(f)
y=sorted(x,reverse=True)
print (y)
elif z=="C":
g=int(input("Input a number you will change: "))
if g in x:
x.remove(g)
print (g, "is removed.")
f=int(input("Put the number you want to replace: "))
x.append(f)
y=sorted(x,reverse=True)
print (y)
elif g not in x:
print ()
print (g, "is not in the list.")
y=sorted(x, reverse=True)
print (y)
elif z=="D":
g=int(input("Input a number you will delete: "))
if g in x:
x.remove(g)
print (g, "is removed.")
y=sorted(x,reverse=True)
print (y)
elif g not in x:
print ()
print (g, "is not in the list.")
y=sorted(x, reverse=True)
print (y)
elif z=="Q":
import sys
print ("Thanks!")
sys.exit
break
else:
print ("Sorry. I could not understand. Try again.")```
【问题讨论】:
-
将列表保存在文件中。您可以使用
json对其进行格式化和解析。 -
您也可以使用 pickle (docs.python.org/3/library/pickle.html) “保存”它(正确的术语是“序列化”)。这可以用于各种 python 对象,例如字典等...
标签: python