0

Very beginner programmer here in the process of learning. I am just wondering if this simple code I have typed is the most optimal way to do it.

with open('guest_book.txt', 'a') as file_object:
    while True:
        name=input("What is your name?")
        print("Welcome " + name + ", have a nice day!")
        file_object.write(name + " has visited! \n")
        another = input("Do you need to add another name?(Y/N)")
        if another == "y":
            continue
        elif another == "n":
            break
        else:
            print("That was not a proper input!")
            while True:
                another = input("Do you need to add another name?(Y/N)")
                if another == "y":
                    a = "t"
                    break
                if another == "n":
                    a = "f"
                    break
            if a == "t":
                continue
            else:
                break

My questions is in the if statements. When I ask the input("Do you need to add another name?(y/n)", is what I have typed the best way to re-ask the question if I get an answer other than y or n. Basically I want the question to be repeated if I don't get either a yes or no answer, and the solution I found does not seem like the most optimal solution.

Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63
lmurdock12
  • 943
  • 1
  • 7
  • 14
  • 1
    Since you're new, review on code is usually done on http://codereview.stackexchange.com/ , please post this there. – ishaan Nov 24 '16 at 06:17
  • Add a "continue" right after the `That was not a proper input` print. – sal Nov 24 '16 at 06:17
  • Possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – tripleee Nov 24 '16 at 06:34
  • @ishaan O I did not realize this, thank you for letting me know though. – lmurdock12 Nov 24 '16 at 21:13

3 Answers3

2

You are basically there. You can simply:

with open('guest_book.txt', 'a') as file_object:
    while True:
        name=input("What is your name?")
        print("Welcome " + name + ", have a nice day!")
        file_object.write(name + " has visited! \n")
        another = input("Do you need to add another name?(Y/N)")
        if another == "y":
            continue
        elif another == "n":
            break
        else:
            print("That was not a proper input!")
            continue
Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63
sal
  • 3,515
  • 1
  • 10
  • 21
0

You can use function to write your all logic at one place.

def calculate(file_object):
    name=raw_input("What is your name?")
    print("Welcome " + name + ", have a nice day!")
    file_object.write(name + " has visited! \n")
    another = raw_input("Do you need to add another name?(Y/N)")
    if another == "y":
        calculate(file_object)
    elif another == "n":
        return
    else:
        print("That was not a proper input!")
        calculate(file_object)

if __name__=='__main__':    
    with open('guest_book.txt', 'a') as file_object:
        calculate(file_object)
Prashant Gaur
  • 9,540
  • 10
  • 49
  • 71
0

You can do it this way, but there will not be any invalid input for saying no. It will only check for saying y

with open('guest_book.txt', 'a') as file_object:
    another = 'y'
    while another.lower() == 'y':
        name=input("What is your name?")
        print("Welcome " + name + ", have a nice day!")
        another = input("Do you need to add another name?(Y/N)")
Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63