Home > Enterprise >  For some reason odd reason i can't seem to make my code write in the txt file
For some reason odd reason i can't seem to make my code write in the txt file

Time:11-27

So i have written this code where i want the computer to open a file and write in it what the user have answered to the question i asked him but when ever i open the txt file its empty.

import os 

Welcome = input("Hi my name is Steve. Do you have an account at Steve? ANSWER WITH JUST A YES OR NO ")

def register():
    name = input("First name: ")
    last_name = input("Last name: ")
    Email = input("Email: ")
    ussername = input("Username: ")
    password = input("Password: ")
def login():
    ussername = input("Username: ")
    password = input("Password: ")

if Welcome == "yes":
    login()
else: 
    register()

if Welcome == "no" or "No":
    with open("userinfo.txt", "w") as file:
        file.write(register())


CodePudding user response:

You are not writing anything to the file. I have modified the code to add the response to the file and also changed the code to be more accurate.

welcome = input("Hi my name is Steve. Do you have an account at Steve? ANSWER WITH JUST A YES OR NO ")


def register():
    first_name = input("First name: ")
    last_name = input("Last name: ")
    email = input("Email: ")
    username = input("Username: ")
    password = input("Password: ")

    with open("userinfo.txt", "w") as file:
        file.write(f"{first_name}\n{last_name}\n{email}\n{username}\n{password}")


def login():
    username = input("Username: ")
    password = input("Password: ")


if welcome.upper() == "YES":
    login()
    print("LOGGED IN!")
elif welcome.upper() == "NO":
    register()
    print("REGISTRATION SUCCESFULL!")
else:
    print("WRONG INPUT!")

CodePudding user response:

Your file is empty because you're not writing anything into it. Your register() function does not return anything, so nothing gets written into the file.

Maybe you want to add something like

return f"{name} {last_name}"

to the end of your register() func? At least then something should get written to your output file.

Also, you have a logic error in if Welcome == "no" or "No":

I would change that to:

if Welcome.lower() == "no":

That fixes your logic error.

The line you wrote could have been written as:

if Welcome == "no" or Welcome == "No":
  • Related