-2

This is my code;

Import os
#hard code the path to the external file
external_file = 'names.txt'
username = 'testuser'# Our hardcoded name

#if file doesn't exists, create a new file
if not os.path.exists(external_file):
    #Ask the user's name
    name = raw_input("What's your name?")
    with open(external_file, "a") as f: # using "a" will append to the file
        f.write(name)# Write the name to names.txt
        f.write("\n")
        f.close()
else:
    #if file exists, use it to load name, else ask user
    with open(external_file, "r+") as f:# r+ open a file for reading & writing
        lines = f.read().split('\n') # split the names 
        print lines
        if username in lines: #Check if the file has any username as 'testuser'
            print "Hi {}".format(username)
        else: # If there is no username as 'testuser' then ask for a name
            name = raw_input("What's your name?")
            f.seek(0,2) # Resolves an issue in Windows
            f.write(name)# Write the name to names.txt
            f.write("\n")
            f.close()

This is good and working code but I don't want it to have a list of names being displayed and it asking you to pick. I want it to store only one name and draw that name everytime the programme is turned on unless you tell it otherwise E.g;

IsName = raw_input("That is your name, right?")
If 'no' in IsName:

Then the code to change the name on file

Korem
  • 11,383
  • 7
  • 55
  • 72

1 Answers1

1

I have modified your code with comments. Is this want you want? This works as below:

  1. The code first check if there is a file named names.txt, if there is no such file it will create a new file names.txt and then ask for a name. Then it will save this name to the file names.txt

  2. If the file names.txt already exists then it will open the file and check if it contains our hardcoded name that is testuser. If it contains the testuser then it will say:

    Hi testuser Is this your name?

If the user enters no then it will ask for a new name and write it to the names.txt file. If the names.txt file exists and it doesn't contains the hardcoded name testuser, then programme will ask for name and write it to the names.txt file.

Code:

import os
#hard code the path to the external file
external_file = 'names.txt'
username = 'testuser'#Our hardcoded name

#if file doesn' exists, create a new file
if not os.path.exists(external_file):
    #Ask the user's name
    name = raw_input("What's your name?")
    with open(external_file, "a") as f: #using "a" will append to the file
        f.write(name)# Write the name to names.txt
        f.write("\n")
        f.close()
else:
    #if file exists, use it to load name, else ask user
    with open(external_file, "r") as f:#r+ opens a file for reading & writing
        lines = f.read().split('\n') # split the names 
        f.close()
    if username in lines: #Check if the file has any username as 'testuser'
        print "Hi {}".format(username)
        response = raw_input("Is this your name?")
        if 'no' in response.lower():
            name = raw_input("What's your name?")# Asks for new name
            with open(external_file, "w") as f:
                f.write(name)# Write the name to names.txt
                f.write("\n")
                f.close()
    else: # If there is no username as 'testuser' then ask for a name
        name = raw_input("What's your name?")
        with open(external_file, "w") as f:# Write name to the file
            #f.seek(0,2) # Resolves an issue in Windows
            f.write(name)# Write the name to names.txt
            f.write("\n")
            f.close()

Modified code:

import os
#hard code the path to the external file
external_file = 'names.txt'

#if file doesn' exists, create a new file
if not os.path.exists(external_file):
    #Ask the user's name
    name = raw_input("What's your name?")
    with open(external_file, "a") as f: #using "a" will append to the file
        f.write(name)# Write the name to names.txt
        f.write("\n")
        f.close()
else:
    #if file exists, use it to load name, else ask user
    with open(external_file, "r") as f:#r+ opens a file for reading & writing
        lines = f.read().split('\n') # split the names 
        f.close()
        print "Hi {}".format(lines[0])
        response = raw_input("Is this your name?")
        if 'no' in response.lower():
            name = raw_input("What's your name?") # Asks for new name
            with open(external_file, "w") as f:
                f.write(name)# Write the name to names.txt
                f.write("\n")
                f.close()
ρss
  • 5,115
  • 8
  • 43
  • 73