#注册函数
def register():
registor_count = 0
while registor_count < 3:
username_inp = input('user name:')
userpwd_inp = input('user password:')
re_userpwd_inp = input('password again:')
if userpwd_inp == re_userpwd_inp:
with open('user_info.txt','a',encoding = 'utf8') as fa:
fa.write(f'{username_inp}:{userpwd_inp}\n')
print('registor successfully')
break
else:
registor_count += 1
print('different password')
continue
#登录
# login_count = 0
# while login_count < 3:
# username_inp = input('user name:')
# userpwd_inp = input('user password:')
#
# with open('user_info.txt','r',encoding='utf8') as fr:
# for user_info in fr:
# user_name,user_pwd =user_info.split(':')
# if user_name.strip() == username_inp :
# if user_pwd.strip() == user_inp:
# print('login successfully')
# break
# else:
# print('error password')
# login_count += 1
# if login_count == 3:
# print('error too many times')
# break
# else:
# print('unfound username')
# #登录
def login():
username_inp = input('user name:')
userpwd_inp = input('user password:')
with open('user_info.txt','r',encoding='utf8') as fr:
for user_info in fr:
user_name,user_pwd =user_info.split(':')
if (user_name.strip() == username_inp and user_pwd.strip() == userpwd_inp):
print('login successfully')
break
else:
print('login is failure')
#猜年龄
def guess_age():
age = 30
guess_count = 0
while guess_count < 3:
guess_age = int(input('guess the age:'))
guess_count += 1
if guess_age > age:
print('too old')
elif guess_age < age:
print('too young')
else:
print('bingo')
break
else:
print('too many errors')
#选择奖品
def prize():
prize_dict = {
'1':'apple',
'2':'orange',
'3':'banana',
'4':'pear'
}
print(prize_dict)
prize_choice = input('choose a prize:')
if prize_choice in prize_dict:
print(f"you got one {prize_dict.get(prize_choice)}")
else:
print('wrong number')
def combo():
while True:
get_inp = input('registor press 1,login press 2,q to quit:')
if get_inp == '1':
register()
login()
guess_age()
prize()
elif get_inp == '2':
login()
guess_age()
prize()
elif get_inp == 'q':
break
else:
print('again')
continue
combo()
转载于:https://www.cnblogs.com/agsol/p/11550674.html