如何讓 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

相關內容