gpt4 book ai didi

python - 无法在 Python 中分配给文字

转载 作者:太空宇宙 更新时间:2023-11-04 10:38:15 28 4
gpt4 key购买 nike

我在 python 中收到一个错误,提示我“无法分配给文字”。我希望有人能帮助我理解为什么会出现这种情况。这是我的代码:

# Chapter 3 Challenge part 3
# use the code below and the following
# directions to see if you can solve the problem

# Your Co-Worker has been asked to write a
# 1st time "Log-in" program in which a user has to
# sign in with their user name. If they enter
# the correct id name they need to be asked
# to create their password.

# They were required to make sure the user did
# not leave the username entry blank and must have the
# correct user name or the progam should re-ask
# them to sign in... They cannot figure out what
# to do, so they have called on you to help...

# Here is what they have so far...
# They started their program like this....
# and included some of their documentation.



# Get username, check to see if not empty,
# then verify that username is correct.
# using username "jalesch" as test user.
name = False
while not name:
name = raw_input("Enter your user name: ")

print "Welcome," , name , "you must create a password."

password = False

while not password:
password = raw_input("""Enter a password.
Password must be at least 6 characters in length,
and must include one of these symbols (!, $, ?, &): """)
while len(password) < 6:
for "!" in password or "$" in password or "?" in password or "&" in password:
password = raw_input("""Enter a password.
Password must be at least 6 characters in length,
and must include one of these symbols (!, $, ?, &): """)

print "This has met the requirements."

check = raw_input("Please re-enter your password:")
while check != password:
print "You have entered the wrong password"
check = raw_input("Please re-enter your password:")
print "Welcome to the program!"


# prompt user to create a password that meets the
# following requirements:
# - Must be at least 6 characters in length
# - Must include at least one of these 4 symbols
# - ! $ ? &
# Once the user has entered their password, the program
# must check to see if it meets the requirements.
# If the requirements are not met, it should reask them
# to put in a password that meets the requirements
# until they get it correct.
# Test password is: $program!

# If it meets the requirements then prompt the user
# to re enter the password, and check that the
# first and second entry matches.
# If the 1st and 2nd match. display "Welcome to the program"
# If the 1st and 2nd don't match, re-prompt for the password
# until they get a correct match.

我还认为当我尝试查找特殊字符时代码出现故障,因为它跳过了一个 while 循环。感谢任何帮助,谢谢。

最佳答案

在这一行:

for "!" in password or "$" in password or "?" in password or "&" in password:

您可能打算使用 if:

if "!" in password or "$" in password or "?" in password or "&" in password:

for x in y 遍历 y,依次将每个成员分配给 x,为每个成员循环运行以下缩进代码y。您不能为 "!" 赋值,因为它是一个文字字符,而不是一个变量。

关于python - 无法在 Python 中分配给文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22312969/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com