在python 2.7中写入文件帮助
这是我的自动取款机,它有pin码保护功能,基本上可以像普通的自动取款机一样运行,但是我需要让它在你每次登出和登入时都能记住你和你的余额。我试着用一个空白的文本文件(文本文件中没有任何内容),并将它链接到我的代码中(如你所见),但它不起作用,我不知道我要补充什么。有什么帮助吗在python 2.7中写入文件帮助,python,python-2.7,Python,Python 2.7,这是我的自动取款机,它有pin码保护功能,基本上可以像普通的自动取款机一样运行,但是我需要让它在你每次登出和登入时都能记住你和你的余额。我试着用一个空白的文本文件(文本文件中没有任何内容),并将它链接到我的代码中(如你所见),但它不起作用,我不知道我要补充什么。有什么帮助吗 balance = float(0) userInput = None path = 'N:\ATM.txt' username = 'some_username' with open(path, 'r') as f
balance = float(0)
userInput = None
path = 'N:\ATM.txt'
username = 'some_username'
with open(path, 'r') as file:
for user in file.readlines():
if user == username:
print("welcome back")
print("Hello, Welcome to the ATM")
print("")
print("Please begin with creating an account")
name = raw_input("Enter your name: ")
saved_code = str(raw_input("Please enter a 4 digit pin to use as your passcode: "))
try:
int(saved_code)
if len(saved_code)!=4:
raise
except Exception, e:
print("Error: Pin is not a valid 4 digit code")
exit()
totalTrails = 3;
currentTrail = 0;
status = 1;
while currentTrail < totalTrails:
user_code =str(raw_input('Please enter the 4 digit pin on your card:'))
if user_code==saved_code:
status=0
break;
else:
currentTrail+=1
if status==0:
print("correct pin!")
else:
print("You tried to enter a wrong code more than three times.")
exit();
print "Hello , welcome to the ATM"
while userInput != "4":
userInput = raw_input("\n what would you like to do?\n\n (1)Check balance\n (2)Insert funds\n" +
" (3)Withdraw funds\n (4)Exit the ATM\n" )
if userInput == "1":
print "your balance is", "£" , balance
elif userInput == "2":
funds = float(raw_input("Enter how much money you want to add"))
balance = balance + funds
elif userInput == "3":
withdraw = float(raw_input("Enter how much money you want to withdraw..."))
balance = balance - withdraw
elif userInput == "4":
print "Thanks for using the ATM!"
balance=float(0)
用户输入=无
路径='N:\ATM.txt'
用户名='some_username'
打开(路径“r”)作为文件:
对于文件.readlines()中的用户:
如果用户==用户名:
打印(“欢迎回来”)
打印(“您好,欢迎使用ATM”)
打印(“”)
打印(“请从创建帐户开始”)
name=原始输入(“输入您的姓名:”)
saved_code=str(原始输入(“请输入一个4位pin作为密码:”))
尝试:
int(保存的_代码)
如果len(已保存的_代码)=4:
提升
除例外情况外,e:
打印(“错误:Pin不是有效的4位代码”)
退出()
totalTrails=3;
currentTrail=0;
状态=1;
当currentTrail
您正在以“r”模式打开文件,这意味着只读,如果要在其中读写,应使用“r+”
在本例中,您没有向文件写入任何内容,这是由write()方法完成的
write(“我想写入文件的字符串”)
写入文件后,将其关闭-file.close()为什么
Java
和Javascript
标记与python
相关?空白文件的意义是什么?你为什么不给文件写信呢?什么不起作用?始终对文件使用和
。