这是我的代码:-
f= open("Passes.py", "a+")
m=open("money.py","a+")
passes= {}
init={}
initial=0
import time
print "Welcome to the virtual banking system"
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:
print "Welcome to your account" + " " + new_user
**useropt=raw_input("Would you like to view your balance- enter 1, deposit money- enter 2, withdraw money- enter 3 or exit- enter 4:")**
if useropt=="1":
print "Your balance is:", initial
m.write(str(initial)+"n")
if useropt=="2":
amountdep= int(raw_input("How much money would you like to deposit?:"))
initial+=amountdep
print "Thanks. Your new balance is:", initial
m.write(str(initial)+"n")
if useropt=="3":
amountwith=int(raw_input("How much would you like to withdraw?:"))
initial-=amountwith
print "Your balance is:", initial
m.write(str(initial)+"n")
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:
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, withdraw money- enter 3 or exit- enter 4:")
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)
print "Your balance is:", t
with open("money.py", "w") as m:
m.write(str(t))
我想知道如何创建一个循环来询问用户是否要选择另一个操作,例如每次操作完成后的 useropt 变量。例如,在每个if语句的末尾,用户选择执行某个操作,我想知道如何再次询问用户是否要执行另一个操作。谢谢。
import time
之后,你可以做:
running = True
while running:
print "Welcome to the virtual banking system"
#the rest of your code, indented by one.
continue = raw_input("Would you like to do another operation? 'Y/N'").lower()
running = continue == "y"
这将为用户提供在每次操作后退出的选项,否则重新启动。
你的代码应该以其他方式结构化,使用 while 循环和一个函数来处理输入。
一般示例:
# this code should go after creating a new account/entering an existing one
while True:
Ans = raw_input("what would you like to do? 1... 2... , -1 to exit")
int code = ProcessAns(int(Ans))
if code == -1:
print "bye!"
break
# and here the function that will be called every time
def ProcessAns(ans):
if ans == 1:
# do stuff
elif ans == 2:
# do other stuff
# include a way to get out
elif ans == -1:
return -1
return 1