Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 代码在没有循环的情况下工作,但不';不能使用循环_Python_Loops - Fatal编程技术网

Python 代码在没有循环的情况下工作,但不';不能使用循环

Python 代码在没有循环的情况下工作,但不';不能使用循环,python,loops,Python,Loops,当我删除不正确的\u credentials2位(循环)时,这段代码可以工作,但当我有它时,它不会显示在shell中。如果accountmanagement==2:条件在while循环中,您需要将条件放入while循环,我们将不胜感激 file = open("Login.txt","a") file.write (newusername1) file.write (",") file.write (newpassword1) file.write("\n") file.close() pri

当我删除不正确的\u credentials2位(循环)时,这段代码可以工作,但当我有它时,它不会显示在shell中。如果accountmanagement==2:条件在while循环中,您需要将
条件放入while循环,我们将不胜感激

file = open("Login.txt","a")
file.write (newusername1)
file.write (",")
file.write (newpassword1)
file.write("\n")
file.close()

print("")

incorrect_credentials2=True 

while incorrect_credentials2:
    username1=input("Please enter your username: ")
    password1=input("Please enter your password: ")
    with open('Login.txt') as f:
        if username1 +","+ password1 in f.read():
            print("Logged In")
            incorrect_credentials2 = False
        else:
            print("Incorrect username or password")
            incorrect_credentials2 = True

如果
条件在while循环之外,则第二个
的缩进。只要进去就可以了。

我想你被困在了第一个while循环中。请参阅更正的代码,其中我在第一个循环中添加了一个条件。


请包括演示问题的内容,如果条件在while循环之外并且您从未退出第一个while循环,请不要链接到外部站点您的第二个while循环。所以你的代码被卡住了。检查下面我的答案中是否有正确的代码。如果他们选择2,这将有一个无限循环(输入2也有一个无限循环,例如accountmanagement的3)是,这将发生,因为我们没有考虑用户输入除
1和2
以外的任何值的情况。更新答案。如果出现任何其他问题,请检查并让我知道。谢谢,这对我的两段代码都有帮助
print("Hello Player 1, you need an account to play Dice!")
print("1: Login to your account")
print("2: Create an account")
accountmanagement = int(input())

print("\n")

incorrect_credentials1= True

while incorrect_credentials1:
    if accountmanagement == 1:
        username1=input("Please enter your username: ")
        password1=input("Please enter your password: ")
        with open('Login.txt') as f:
            if username1 +","+ password1 in f.read():
                print("Logged In")
                incorrect_credentials1=False
            else:
                print("Incorrect username or password")
                incorrect_credentials1=True


    if accountmanagement == 2:
        newusername1 = input("Please enter your name: ")
        newpassword1 = input("Excellent! Now let's create a password: ")

        file = open("Login.txt","a")
        file.write (newusername1)
        file.write (",")
        file.write (newpassword1)
        file.write("\n")
        file.close()
        incorrect_credentials1=False
    else:
        print("Incorrect option selected.)
        incorrect_credentials1 = False
if accountmanagement == 1:
  while incorrect_credentials1:
      username1=input("Please enter your username: ")
      password1=input("Please enter your password: ")
      with open('Login.txt') as f:
        if username1 +","+ password1 in f.read():
          print("Logged In")
          incorrect_credentials1=False
        else:
          print("Incorrect username or password")
          incorrect_credentials1=True