これが私のコードです:-
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."
2 つのファイルがあり、そのうちの 1 つは「money.py」です。このファイルには、ユーザーのアカウントの合計金額が保存されます。ただし、問題は、プログラムでは現在 2 人のユーザーとその金額を区別する方法がなく、合計金額の整数値のみを保存することです。ユーザー名と金額の順に記述することも試しましたが、文字列と整数が混在するため、うまくいきませんでした。異なるユーザーを区別する方法はありますか? よろしくお願いします。
答え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