很抱歉,如果这是发布此信息的错误区域。自从我接触python(一般代码)以来已经有一段时间了,所以我提供的代码很可能是可怕的,但它不会将变量line1和line2写入USERDATA.py文件,而且我不知道为什么,任何帮助都是赞赏。干杯
#import
import os
#
runstartText = 'Start success'
print(runstartText)
#setting values
file1 = './data'
file2 = './data/userpass'
file3 = './data/userpass/access.py'
newUserTEXT1 = 'Please input a valid email, username and password'
#checking/creating directorys
if not os.path.exists(file1):
os.makedirs(file1)
print('Created', file1)
if not os.path.exists(file2):
os.makedirs(file2)
print('Created', file2)
#def
def newUser():
print(newUserTEXT1)
username = input('Enter New System Username: ')
password = input('Enter New System Password: ')
passwordConfirm = input('Re-Enter New System Password: ')
if password == passwordConfirm:
print('Username and Password Stored')
line1 = 'username = ', username
line2 = 'username = ', password
apples = 1
if apples == 1:
userdatafile = open('USERDATA.py', 'w+')
userdatafile.writelines([line1, line2,])
userdatafile.close()
#-> store data
#-> to login
if password != passwordConfirm:
print('ERROR: Passwords do not match, please retry')
#-> restart newUser string
def login():
from USERDATA import username, password
print('Please Enter Username and Password:')
usernameTEMP = input('Username: ')
if usernameTEMP == username:
passwordTEMP = input('Password: ')
if passwordTEMP == password:
print('Successful Login!')
# -> to login
答案 0 :(得分:0)
在您的代码中,您从未真正调用过newUser()
。我添加了一个电话并收到错误消息:
line 37, in newUser
userdatafile.writelines([line1, line2,])
TypeError: write() argument must be str, not tuple
因此,我们应将userdatafile.writelines([line1, line2,])
中的元组替换为字符串userdatafile.writelines("".join(line1).join(line2))
一切都会按预期进行。
答案 1 :(得分:0)
正如Hoog早已指出的那样,最主要的问题是您没有调用newUser
函数。始终在问题中显示所有相关代码,并去除不必要的细节。
我在您的代码中看到的下一个最大问题是,您没有构建要写入文件的字符串。我猜line1
和line2
的定义应该像这样:
line1 = 'username = ' + username + '\n'
line2 = 'password = ' + password + '\n'
一个重要警告:希望您仅作为练习来这样做。这甚至不是处理帐户数据的正确方法。