首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >跳过if命令和and命令

跳过if命令和and命令
EN

Stack Overflow用户
提问于 2017-10-28 14:38:04
回答 2查看 62关注 0票数 0
代码语言:javascript
复制
print ("Welcome to my Quiz!")
existing = input("Are you an existing user?: ")
if existing.lower == "yes":
    print("Enter your credidentials")
username= input("Enter your Username: ")
password= input("Enter your Password: ")
file= open("data.txt", "r")
found=False
for line in file:
    account = line.split(",")
    if account[0] == username:
        password= existing[1]
        found=True
file.close()
if found==True:
    print("Welcome Back", username ,)
if found==False:
    print("Account not found")
else:
    existing.lower == "no"
    user= input("Enter your first name: ")
    year= input("Enter the year you are in: ")
    password= input("Enter your password: ")
    username=user[:2]+year      
    writefile=open("data.txt","a")
    writefile.write(username + "," + password + "\n")
    writefile.close() 
    print("Your account has been created." "Your username is..", username , "..and your password is", password,)

我试着做一个测试,我希望它的工作方式是,如果他们的用户有一个帐户,它不会经过建立一个新帐户的过程。现在,如果我登录,它仍然向我询问我的名字等等,完全跳过了ifelse命令。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-28 14:52:05

有几个问题

  1. 如果还有其他对齐问题
  2. 调用下函数
  3. 读取行(不删除新行字符)

试一试,并提供反馈

代码语言:javascript
复制
print ("Welcome to my Quiz!")
existing = input("Are you an existing user?: ")
if existing.lower()[0] == "y":
    print("Enter your credidentials")
    username= input("Enter your Username: ")
    password= input("Enter your Password: ")
    file= open("data.txt", "r")
    found=False
    for line in file:
        account = line.strip().split(",")
        if account[0] == username:
            password= account[1]
            found=True
    file.close()
    if found==True:
        print("Welcome Back", username )
    else:
        print("Account not found")
else:
    #print("Account not found")
    #existing.lower() == "no"
    user= input("Enter your first name: ")
    year= input("Enter the year you are in: ")
    password= input("Enter your password: ")
    username=user[:2]+year      
    writefile=open("data.txt","a")
    writefile.write(username + "," + password + "\n")
    writefile.close() 
    print("Your account has been created." "Your username is..", username , "..and your password is", password,)
票数 1
EN

Stack Overflow用户

发布于 2017-10-28 14:56:49

试试这个:

代码语言:javascript
复制
print("Welcome to my Quiz!")

existing = input("Are you an existing user?: ")


if existing.lower() == "yes":
    print("Enter your Credidentials")

    username = input("Enter your Username: ")
    password = input("Enter your Password: ")

    with open("data.txt", "r") as raw_data:
        my_data = raw_data.read()

    re_data = my_data.split('\n')

    check_box = username + "," + password

    if check_box in re_data:
        print("Welcome Back " + username)

    else:
        print("Account not found")


else:

    user = input("Enter your first name: ")
    year = input("Enter the year you are in: ")
    password = input("Enter your password: ")
    username = user[:2]+year

    save_box = username + "," + password + "\n"

    with open("data.txt", "a") as raw_data:
        raw_data.write(save_box)

    print("Your account has been created." "Your username is.." + username + "..and your password is" + password)


print("Goodbye, Have a Nice Day ...")

您的username_password文件应该是:

代码语言:javascript
复制
jack,6354734346534
edward,45645646754
jeje,874574574587
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46990885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档