Home > front end >  Not able to continue if-statement when checking if file already exists
Not able to continue if-statement when checking if file already exists

Time:05-23

I am trying to code a programme where user can create a profile, modify it, review it and so on. After all this information is gathered, the data should be saved in a file.

My code was alright until I added this last bit. I am not able to create a condition where my programme checks if the file has already been created or not. If the file already exists, a message should be thrown to user saying so. If the file was not created, the programme should carry on asking user all the questions to create the profile.

I am a bit stuck. With this code, the part of "What's your name" is duplicated. The programme is asking twice the user his/her name and I am not able to figure out how to fix it. This is what I have done so far:

import os

def ask_option():
    # Print menu and return chosen option
    print("\nMENU\n")
    print("******************")
    print("Create new profile, click 1.")
    print("Modify profile, click 2.")
    print("Review profile, click 3.")
    print("Updating currently binge watchting TV show, click 4.")
    print("Leave the programme, click 5.")

    option = int(input("\nWhat do you what to do?\n"))
    print("\n******************\n")
    return option 

def ask_data():
    # Ask and return all the data provided by user
    name = input("What's your name? ").capitalize()
    surname = input("What's your surname?: ").capitalize()
    year = int(input("Which year were you born? "))
    age = 2022 - year
    
    return name, surname, year, age

def print_data(name, surname, year, age):
    # Print all the data provided by user
    profile = name, surname, year, age
    print()
    print("Name:", name, surname)
    print("Age:", age, "years old")
    return profile  

def save_data(name, surname, year, age):
    # Guarda los datos del usuario en un archivo
    file = open(name   ".user", "w", encoding = "UTF-8")
    file.write(name   "\n"   surname   "\n"   age   "\n")
    file.close()

def create_profile():
# Check if file with user data already exists. If that is so, return message. If there is no file, carry on asking user data to create profile
    carryon = True
    while carryon:
    name = input("What's your name? ").capitalize()
    if not os.path.isfile(name   ".user"): 
          name, surname, year, age = ask_data()
          save_data(name, surname, year, age)
          print("\nYour profile has been created.\n")
          print_data(name, surname, year, age)
          carryon = False
    else:
          print("Your profile already exists.")
        

CodePudding user response:

A few issues with the code continue is a keyword and should be reserved. Also there is no need for a while as continue will handle this well.

    def create_profile():
            name = input("What's your name? ").capitalize()
            if os.path.isfile(name   ".user"):
                print("Your profile already exists")
                continue
            name, surname, year, age = ask_data()
            save_data(name, surname, year, age)
            print("\nYour profile has been created.\n")
            print_data(name, surname, year, age)

CodePudding user response:

You don't need a loop here, right? Either it exists, and you return, or it doesn't exist, and you create it.

def create_profile():
    # Check if file with user data already exists. If that is so, return message. If there is no file, carry on asking user data to create profile
    name = input("What's your name? ").capitalize()
    if os.path.isfile(name   ".user"):
        print("Your profile already exists.")
        return
 
    name, surname, year, age = ask_data()
    save_data(name, surname, year, age)
    print("\nYour profile has been created.\n")
    print_data(name, surname, year, age)
  • Related