我怎样才能让 python 区分我的“money.py”文件中的不同用户?

我怎样才能让 python 区分我的“money.py”文件中的不同用户?

这是我的代码:-

f= open("Passes.py", "a+")
m=open("money.py","a+")
passes= {}
init={}
initial=0
import time
wait=time.sleep(0.5)
print "Welcome to the virtual banking system"
wait
user=raw_input("Would you like to create a new account? 'Y/N'").lower()
if user== "y":
  new_user= raw_input("Create a username:")
  new_pass= raw_input("Create a password:")
  p= passes[new_user]= new_user + ":" + new_pass
  f.write("\n"+p)
  ask=raw_input("Would you like to sign into your account? 'Y/N'").lower()
  if ask=="y":
    user_in=raw_input("Enter your username:")
    if user_in==new_user:
      pass_in=raw_input("Enter your password:")
      if pass_in==new_pass:
        running=True
        while running:
          print "Welcome to your account" + " " + new_user
          useropt=raw_input("Would you like to view your balance- enter 1, deposit money- enter 2 or withdraw money- enter 3:")
          if useropt=="1":
            print "Your balance is:", initial
            m.write("\n"+str(initial))
          if useropt=="2":
            m.close()
            u=open("money.py","r+")
            amountdep= int(raw_input("How much money would you like to deposit?:"))
            initial+=amountdep
            print "Thanks. Your new balance is:", initial
            u.write(str(initial)+"\n")
          if useropt=="3":
            m.close()
            r=open("money.py","r+")
            amountwith=int(raw_input("How much would you like to withdraw?:"))
            initial-=amountwith
            if initial>=0:
              print "Your balance is:", initial
              r.write(str(initial)+"\n")
            else:
              print "Sorry, this amount cannot be withdrawn. Balance cannot be less than 0"
          cont = raw_input("Would you like to do another operation? 'Y/N'").lower()
          running = cont == "y"
      else:
        print "Password not valid"

    else:
      print "Username does not exist"

  else:
    print "Thanks for using the virtual bank."

else:
  user2=raw_input("Do you have an existing account? 'Y/N'").lower()
  if user2=="y":
    existing_user=raw_input("Enter your username:")
    exisitng_pass=raw_input("Enter your password:")
    for passwords in f:
      if passwords==existing_user+":"+exisitng_pass:
        run=True
        while run:
          print "Welcome to your account" + " " + existing_user
          with open("money.py", "r") as m:
            info= int(m.readline().strip())
            useropt2=raw_input("Would you like to view your balance- enter 1, deposit money- enter 2 or withdraw money- enter 3:")
            if useropt2=="1":
              print "Your balance is:", info
            if useropt2=="2":
              amountdep= int(raw_input("How much money would you like to deposit?:"))
              a=info+int(amountdep)
              print "Your new balance is:", a
              with open("money.py", "w") as m:
                  m.write(str(a))
            if useropt2=="3":
              amountwith=int(raw_input("How much would you like to withdraw?:"))
              t=info-int(amountwith)
              if t>=0:
                print "Your balance is:", t
                with open("money.py", "w") as m:
                  m.write(str(t))
              else:
                print "Sorry, this amount cannot be withdrawn. Balance cannot be less than 0"
            restart = raw_input("Would you like to do another operation? 'Y/N'").lower()
            run = restart == "y"
      else:
        print "Invalid username or password"
  else:
    print "Thanks for using the virtual bank."

我有两个文件,其中一个是“money.py”。在这个文件中,存储了我用户帐户的总金额。但问题是,目前我的程序无法区分两个用户及其资金,因为它只存储了他们总金额的整数值。我也尝试过先写入用户名,然后写入金额,但发现这不起作用,因为它混合了字符串和整数。有没有办法区分不同的用户?谢谢。

答案1

这个问题最好放到 stackoverflow 上问。

我的建议是将您的余额存储在字典中,例如balances[user] = amount,然后使用json模块从文件中读取/写入它

加载:

with open("money.py", "r") as moneyfile:
    balances = json.load(moneyfile)

保存:

with open("money.py", "w") as moneyfile:
    json.dump(balances, moneyfile)

注意:.py是用于python源代码的扩展,用于存储数据的另一个扩展是首选,例如.txt.dat

相关内容