二手产品经理

二手产品经理

THIS IS RENO

操作文件 - 53天 - 在线学python100天

记录#

  1. 今天没有新的知识点学习,是对操作文件的综合练习。
  2. 今天的题目是在一个小游戏,存储用户输入的魔法道具。
  3. 今天遇到最大的困难就是 list.remove (),还有就是题目理解的不正确。

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()
加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。