我将如何定义名称验证

How would I do a definition for a name validation

我想做的事情:

我正在尝试定义一个接受输入(名称为字符串)并检查名称是否在集合列表中的定义。

如果是,程序将继续。 如果不是,它将为用户提供 2 次正确输入其姓名的机会。

要求:

我需要在定义后使用name变量。 名字错3次我要系统退出

问题:

如果名称正确,它就可以正常工作 但是,如果名称错误,则不允许输入其他名称,打印“您还有 2 次尝试”和“您还有 1 次尝试”然后结束循环并退出。

代码:

names_allowed_to_play = ["MUM","DAD"]

def val_1 (name_1):

    print("Player 1, you have 3 tries to enter the correct name")
    print("")
    a = 0
    
    while a < 3:

        
        name_1 = name_1.upper()

        if name_1 in names_allowed_to_play:
            print(name_1 + ", you are authorised to play, have fun!")
            print("")
            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")
                print("")
                print("")
            elif name_1 not in names_allowed_to_play and a ==2:
                print("You have 1 more try")
                print("")
                print("")

    if a == 3:
        print("")
        print("Sorry Player 2, " + name_1 + " ruined it! " + name_1 + ", you are NOT AUTHORISED!")
        sys.exit()
        


#Run definition
name_1 = input("Player 1, please enter your name to play: ")
val_1(name_1)

我已经解决了你的代码问题。

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 + ",you are authorised to play, 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()

您的代码存在一些问题:

  • 你永远不会在循环中要求用户输入新的内容,而只是再次测试名字
  • 您修改了(本地)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, 3, 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-statement 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)