如何确保在我的代码(python)中只获得特定的输出?

如何确保在我的代码(python)中只获得特定的输出?,python,for-loop,if-statement,while-loop,output,Python,For Loop,If Statement,While Loop,Output,我正在编写一个代码,其中包含一个登录子例程,它工作得很好,除了当我只需要其中一个输出时,它会给出两个输出。子程序如下所示: def Login(): chances = 0 Status = True #Status refers to whether the person is logged in or not while Status is True: Supplied_Username = input('What is your usernam

我正在编写一个代码,其中包含一个登录子例程,它工作得很好,除了当我只需要其中一个输出时,它会给出两个输出。子程序如下所示:

def Login():

    chances = 0 
    Status = True #Status refers to whether the person is logged in or not

    while Status is True:
        Supplied_Username = input('What is your username?')
        Supplied_Password = input('What is your password?')
        with open("Loginfile.txt","r") as Login_Finder:
            for x in range(0,100):

                for line in Login_Finder:

                    if (Supplied_Username + ',' + Supplied_Password) == line.strip():  
                        print("You are logged in")
                        game()
            else:
                print("Sorry, this username or password does not exist please try again")
                chances = chances + 1
                if chances == 3:
                    print("----------------------------------------------------\n Wait 15 Seconds")
                    time.sleep(15)
                    Login()
                    sys.exit()

def game():
    print('HI')
这就像我上面说的一样。当用户输入正确的详细信息时,他们将获得以下两种信息:

“您已登录”输出和“抱歉…”。。。“输出”中不存在这些详细信息


我需要做什么来确保为每个场景获得正确的输出(错误的细节和正确的细节)?

我对代码做了一些更改,保持了功能不变,只是展示了一些python最佳实践

(注意,在python中,按照约定,大写名称保留给类名,而snake case用于变量名和函数名)

但是,这并不能解决您的问题

问题是您没有退出此功能。在python中,函数应该
在完成时返回
一些内容,但这里要么开始游戏,要么抛出异常。在许多方面,此函数永远不应该有返回值


也许你想
返回game()
。这将退出函数并调用
game()

范围(01000)内x的
循环的目的是什么?任何事情都不能使用
x
。如果循环正常结束,而不是使用
break
停止,则执行循环的
else:
块。所以,当你找到你想要的东西时,你需要打破循环Python@MadPhysicist这个问题专门询问输出,而不是返回值。每次递归调用
Login()
,它都会将
几率设置为
0
,因此它永远不会达到3。
def login(remaining_chances=3, delay=15):
    '''Requests username and password and starts game if they are in "Loginfile.txt"'''

    username = input('What is your username?')
    password = input('What is your password?')

    with open("Loginfile.txt","r") as login_finder:
        for line in login_finder.readlines():
            if line.strip() == f'{username},{password}':
                print("You are logged in")
                game()

    print("Sorry, this username or password does not exist please try again")

    remaining_chances -= 1

    if remaining_chances == 0:
        print(f"----------------------------------------------------\n Wait {delay} Seconds")
        time.sleep(delay)
        login(delay=delay)
    else:
        login(remaining_chances=remaining_chances, delay=delay)