lmurdock12
lmurdock12

Reputation: 1021

Is there anyway way to shorten this?

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.

Upvotes: 0

Views: 76

Answers (3)

Sardorbek Imomaliev
Sardorbek Imomaliev

Reputation: 15400

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

Upvotes: 0

sal
sal

Reputation: 3593

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

Upvotes: 2

Prashant Gaur
Prashant Gaur

Reputation: 9828

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)

Upvotes: 0

Related Questions