因为文件已经存在,所以要求用户创建新的用户名
ask the user to make new username since the file already existed
基于程序。它可以根据用户输入的用户名保存文件。由于用户名已经存在,如何让程序要求用户更改为其他用户名。
while True:
RegOrLog = raw_input('[Register/Login]:')
if RegOrLog not in ('reg', 'register', 'REGISTER','log', 'login', 'LOGIN',\
'r','l'):
print "Please enter 'register' if you dont have account"
print "Please enter 'login' if you already registered"
if RegOrLog in ('reg', 'register', 'REGISTER','r','Register'):
count = 0
while count < 1:
regusrnm = raw_input('Enter your desire login name:')
regusrnm = regusrnm.lower()
regpswrd = raw_input('Enter your password:')
while True:
regage = raw_input('Enter your age:')
if int(regage) not in range(1, 100):
print 'please enter apropriate value'
else:
break
while True:
reggender = raw_input('State your gender:')
if reggender not in ('male', 'female'):
print 'Are you an alien?'
else:
break
data = {"username": regusrnm, "password": regpswrd, "age": regage,\
"gender": reggender}
pickle.dump( data, open( regusrnm, "wb" ) )
RegOrLog = 'login'
print 'Please login to your account for security purpose'
break
if RegOrLog in ('log', 'login', 'LOGIN','l','Login'):
logusrnm = raw_input('Enter your username:')
data = pickle.load( open( logusrnm, "rb" ) )
count = 3
while count >= 1:
logpswrd = raw_input('Enter your password:')
count = count - 1
if logpswrd != data["password"]:
print "You have",count, "attempt left"
if count == 0:
print "The account doesn't exist or wrong password"
print 'Please register a new one'
break
else:
print 'Welcome Back',data["username"]
break
以及如何将保存文件存储在文件夹中,以免弄得乱七八糟。喜欢干净整洁:D
import os.path
os.path.exists(file_name)
Returns True
如果名称为 file_name
的目录或文件存在。所以你只需检查
if os.path.exists(regusrnm) == True:
# ask user to enter another name
这意味着你改变了
if RegOrLog in ('reg', 'register', 'REGISTER','r','Register'):
count = 0
while count < 1:
到
if RegOrLog in ('reg', 'register', 'REGISTER','r','Register'):
count = 0
while os.path.exists(regusrnm) == True:
在此之前,您需要为 regusrnm
分配一些值。 admin
似乎是一个不错的选择,因为每个系统都有一个 admin
:)
所以整个部分是
regusrnm = 'admin'
if RegOrLog in ('reg', 'register', 'REGISTER','r','Register'):
count = 0
while os.path.exists(regusrnm) == True:
您还可以使用 os.path.isfile(file_name)
来检查 file_name
是否是文件(而不是目录)。
基于程序。它可以根据用户输入的用户名保存文件。由于用户名已经存在,如何让程序要求用户更改为其他用户名。
while True:
RegOrLog = raw_input('[Register/Login]:')
if RegOrLog not in ('reg', 'register', 'REGISTER','log', 'login', 'LOGIN',\
'r','l'):
print "Please enter 'register' if you dont have account"
print "Please enter 'login' if you already registered"
if RegOrLog in ('reg', 'register', 'REGISTER','r','Register'):
count = 0
while count < 1:
regusrnm = raw_input('Enter your desire login name:')
regusrnm = regusrnm.lower()
regpswrd = raw_input('Enter your password:')
while True:
regage = raw_input('Enter your age:')
if int(regage) not in range(1, 100):
print 'please enter apropriate value'
else:
break
while True:
reggender = raw_input('State your gender:')
if reggender not in ('male', 'female'):
print 'Are you an alien?'
else:
break
data = {"username": regusrnm, "password": regpswrd, "age": regage,\
"gender": reggender}
pickle.dump( data, open( regusrnm, "wb" ) )
RegOrLog = 'login'
print 'Please login to your account for security purpose'
break
if RegOrLog in ('log', 'login', 'LOGIN','l','Login'):
logusrnm = raw_input('Enter your username:')
data = pickle.load( open( logusrnm, "rb" ) )
count = 3
while count >= 1:
logpswrd = raw_input('Enter your password:')
count = count - 1
if logpswrd != data["password"]:
print "You have",count, "attempt left"
if count == 0:
print "The account doesn't exist or wrong password"
print 'Please register a new one'
break
else:
print 'Welcome Back',data["username"]
break
以及如何将保存文件存储在文件夹中,以免弄得乱七八糟。喜欢干净整洁:D
import os.path
os.path.exists(file_name)
Returns True
如果名称为 file_name
的目录或文件存在。所以你只需检查
if os.path.exists(regusrnm) == True:
# ask user to enter another name
这意味着你改变了
if RegOrLog in ('reg', 'register', 'REGISTER','r','Register'):
count = 0
while count < 1:
到
if RegOrLog in ('reg', 'register', 'REGISTER','r','Register'):
count = 0
while os.path.exists(regusrnm) == True:
在此之前,您需要为 regusrnm
分配一些值。 admin
似乎是一个不错的选择,因为每个系统都有一个 admin
:)
所以整个部分是
regusrnm = 'admin'
if RegOrLog in ('reg', 'register', 'REGISTER','r','Register'):
count = 0
while os.path.exists(regusrnm) == True:
您还可以使用 os.path.isfile(file_name)
来检查 file_name
是否是文件(而不是目录)。