while != "specific string" 循环事故

While != "specific string" loop mishap

我正在努力使任务更有效率。我要继续research/edit的同时继续前进。

代码当前所做的是询问您的 first/last 名称,然后输出用户名和密码。到目前为止,如果有更有效的方法来重写它,它工作得很好。也很感激。

我唯一需要修复的是循环条目(First_name 和 Last_name),这样我就可以添加多个用户 \n\n 以方便 reading/separation每个用户的。最好使用退出命令,例如“完成”

我还将研究如何创建存储此信息的新文件,但这是我要尝试弄清楚的部分。

First_name = input("Enter your First name: ")
Last_name= input("Enter your last name: ")


Username = str(First_name) + str(Last_name)
Password = Username + str('1!')


print("Username: ", Username.upper())
print("Password: ", Password)

在尝试解决问题方面。

我确实尝试在球场上创造一些东西..

First_name = input("Enter your First name: ")
Last_name= input("Enter your last name: ")

while First_name != "done":
    Last_name= input("Enter your last name: ")
    First_name = input("Enter your First name: ")
    Last_name= input("Enter your last name: ")
    Username = str(First_name) + str(Last_name)
    Password = Username + str('1!')
    print("Username: ", Username.upper())
    print("Password: ", Password)

这只是重复前两行而不打印 Username/password。当我输入完成但与预期略有不同时,它似乎也承认了。下面我包括代码 运行

Enter your First name: first
Enter your last name: last
Enter your last name: last
Enter your First name: done
Enter your last name: last
Username:  DONELAST
Password:  donelast1!

我明白为什么它是第一个、最后一个、最后一个、第一个最后一个。我不知道如何使用“while Fist_name !='done':”来制作 while 循环,而无需将第 1 行和第 2 行放在循环之前。我尝试删除第 5 行和第 6 行的输入,但它只是循环了用户名和密码。

只需在循环开始前将变量设置为空字符串即可。由于空字符串不等于 done 您的代码将进入循环并提示用户:

First_Name = ''
Last_Name = ''
while First_name != "done":
    Last_name= input("Enter your last name: ")
    First_name = input("Enter your First name: ")    
    Username = str(First_name) + str(Last_name)
    Password = Username + str('1!')
    print("Username: ", Username.upper())
    print("Password: ", Password)

您可以使用 while 循环并在名字为 done 时中断它。

while True:
    first_name = input("Enter your first name (done to exit): ")
    if first_name.lower() == "done":
        print("Done!")
        break
    last_name = input("Enter your last name: ")
    Username = str(first_name) + str(last_name)
    Password = Username + str('1!')
    print("Username: ", Username.upper())
    print("Password: ", Password)
    print("\n")

输出:

Enter your first name (done to exit): John
Enter your last name: Doe
Username:  JOHNDOE
Password:  JohnDoe1!


Enter your first name (done to exit): Alice
Enter your last name: Court
Username:  ALICECOURT
Password:  AliceCourt1!


Enter your first name (done to exit): done
Done!