问题描述
我想做什么:
我正在尝试定义一个接受输入(名称作为字符串)并检查名称是否在设置列表中的定义。
如果是,该程序将继续。 如果不是,它将为用户提供两次输入正确名称的机会。
要求:
我需要在定义后使用name变量。 如果名称错误3次,我需要退出系统。
问题:
如果名称正确,则可以正常工作 但是,如果名称错误,则不允许输入其他名称,打印“您还有2次尝试”和“您还有1次尝试”,然后结束循环并退出。
代码:
vector
解决方法
您的代码有一些问题:
- 您永远不会在循环中要求用户输入新内容,而只是再次测试名字
- 您修改(本地)
name_1
变量,但不要return
修改给调用者的值 - 您不必重复所有条件,可以使用数学来确定剩余的尝试次数
您可以尝试以下操作:
def get_name(player,tries,names_allowed_to_play):
print(f"Player {player},you have {tries} tries to enter the correct name")
for i in range(1,tries+1):
name = input("Please enter your name to play: ").upper()
if name in names_allowed_to_play:
print("You are authorised to play,have fun!")
names_allowed_to_play.remove(name)
return name
elif i < tries:
print(f"You have {tries - i} more tries")
else:
print("You messed up")
exit()
names_allowed_to_play = ["MUM","DAD"]
name1 = get_name(1,3,names_allowed_to_play)
name2 = get_name(2,names_allowed_to_play)
print(name1,name2)
,
这对我有用,我认为您的结构方式意味着逻辑存在缺陷。
import sys,time
names_allowed_to_play = ["MUM","DAD"]
def main():
authorised = False
attempts = 3
while authorised == False:
if attempts < 3:
print("You have",attempts,"remaining.")
if attempts > 0:
name = input("Player 1,please enter your name to play: ")
name = name.upper()
elif attempts <= 0:
print("You are locked out.")
time.sleep(1)
sys.exit()
if name in names_allowed_to_play:
print(name," you are authorised to play,have fun!")
names_allowed_to_play.remove(name)
authorised = True
else:
attempts -= 1
main()
,
主要更改:
- 将 input()放入while循环,以便在名称错误时可以获取新输入,否则只能获取一次输入,并且if语句会检查错误的名称3次
- 为允许的名称添加 return 语句,以便稍后使用该名称(例如打印输出或其他功能)
您可以在我的代码注释中找到其他小的更改。
# change the names to lowercase so you don't need upper() for input
names_allowed_to_play = ["mum","dad"]
def player_validate():
# \n for new line,just works like your print("")
print("Player 1,you have 3 tries to enter the correct name\n")
tries = 0
while tries < 3:
player_name = input("Player 1,please enter your name to play: ")
if player_name in names_allowed_to_play:
print(player_name + ",you are authorised to play,have fun!\n")
names_allowed_to_play.remove(player_name)
# return will stop the loop so you don't need to break it by other code
return player_name
else:
tries = tries + 1
# use calculation instead of many elif statment
print(f"You have {3-tries} tries.")
# I have change Player 2 to Player 1,if it is not typo,you can change it back
print("Sorry Player 1," + player_name + " ruined it! " + player_name + ",you are NOT AUTHORISED!")
# use exit() instead of sys.exit() or you will need to import sys at beginning
exit()
# Run function (I think function is a more common name then definition)
name_to_use_later = player_validate()
print(name_to_use_later)
,
我已经解决了您的代码问题。
names_allowed_to_play = ["MUM","DAD"]
def val_1():
print("Player 1,you have 3 tries to enter the correct name")
name_1 = input("enter your name")
a=0
while a < 3:
name_1 = name_1.upper()
if name_1 in names_allowed_to_play:
print(name_1 + ",have fun!")
a = a + 4
names_allowed_to_play.remove(name_1)
elif name_1 not in names_allowed_to_play:
a = a + 1
if name_1 not in names_allowed_to_play and a == 1:
print("You have 2 more tries")
name_1 = input("enter your name")
print("")
elif name_1 not in names_allowed_to_play and a == 2:
print("You have 1 more try")
name_1 = input("enter your name")
elifa == 3:
print("")
print("Sorry Player 2," + name_1 + " ruined it! " + name_1 + ",you are NOT AUTHORISED!")
exit()
val_1()