Record#
- Today there is no new knowledge point to learn, it is a comprehensive exercise on file operations.
- Today's task is to create a small game that stores user-inputted magic items.
- The biggest difficulty encountered today is the use of list.remove(), and also misunderstanding the task.
CODE#
import os, time
try:
f=open("gamelist.txt","r")
gamelist=eval(f.read())
f.close
except:
gamelist = []
def gameadd():
gname = input("Input the item to add: > ").capitalize()
gamelist.append(gname)
print(f"{gname} has been added to your inventory.")
def gameremove():
if gamelist:
gname=input("Input the item to remove: > ").capitalize()
if gname in gamelist:
gamelist.remove(gname)
print(f"{gname} has been removed from your inventory.")
else:
print(f"{gname} is not in the list.")
else:
print("The list is empty")
def gameview():
if gamelist:
gname=input("Input the item to view: > ").capitalize()
if gname in gamelist:
gamenum = gamelist.count(gname)
print(f"You have {gamenum} {gname}.")
else:
print(f"{gname} is not in the list.")
else:
print("The list is empty")
while True:
time.sleep(1)
os.system("clear")
print("🌟RPG Inventory🌟")
menu=input("1: Add\n2: Remove\n3: View\n")
if menu == "1":
gameadd()
elif menu == "2":
gameremove()
elif menu == "3":
gameview()
else:
print("input error")
f=open("gamelist.txt","w")
f.write(str(gamelist))
f.close()
Translation: