Ak.
Ak.

Reputation: 11

Skipping if and else commands

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,)

Im trying to make a quiz and how i want it to work is that if they user has an account it doesn't go through the process of making a new account. Right now, if i login, it still asks me for my firstname etc, completely skipping the if and else commands.

Upvotes: 0

Views: 72

Answers (2)

DRPK
DRPK

Reputation: 2091

Try This:

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 ...")

your username_password file should be:

jack,6354734346534
edward,45645646754
jeje,874574574587

Upvotes: 0

Sandeep Lade
Sandeep Lade

Reputation: 1943

Couple of problems are there

  1. if else alignment problem as well
  2. calling lower function
  3. reading line ( not stripping new line character)

Try this and provide feedback

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,)

Upvotes: 1

Related Questions