Python 有没有办法缩短这个时间?

Python 有没有办法缩短这个时间?,python,Python,非常初级的程序员在这里学习的过程。我只是想知道我输入的这个简单的代码是否是最理想的方法 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 visit

非常初级的程序员在这里学习的过程。我只是想知道我输入的这个简单的代码是否是最理想的方法

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

我的问题在if声明中。当我询问输入时(“您需要添加其他名称吗?(y/n)”,是我键入的内容,如果我得到的答案不是y或n,则是重新提问的最佳方式。基本上,如果我没有得到“是”或“否”的答案,我希望重复提问,并且我找到的解决方案似乎不是最优解。

你基本上就是这样。你可以简单地:

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

你基本上就在那里。你可以简单地:

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

您可以使用该函数在一个位置写入所有逻辑

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)

您可以使用该函数在一个位置写入所有逻辑

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)

您可以这样做,但不会有任何无效的输入来表示不。它只会检查是否表示
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)")

您可以这样做,但不会有任何无效的输入来表示不。它只会检查是否表示
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)")

由于您是新手,代码审查通常在上完成,请将此发布在那里。在输入不正确的
之后添加一个“continue”(继续)。可能重复@ishaan O我没有意识到这一点,谢谢您让我知道。由于您是新手,代码审查通常在上完成,请将此发布在那里。添加一个“continue”(继续)就在
之后,这不是一个正确的输入
打印。可能是@ishaan O的重复。我没有意识到这一点,谢谢你让我知道。