-1

What I want to do:

I am trying to make a definition that takes an input (name as string) and checks if the name is in a set list.

If it is, the program will continue. If it isn't, it will give the user 2 more chances to enter their name correctly.

Requirements:

I need to use the name variable after the definition. I need the system to exit if the name is wrong 3 times.

Problems:

If the name is correct it works properly However, if the name is wrong, it doesn't allow another name input, printing "You have 2 more tries" and "You have 1 more try" then ends the loop and exits.

Code:

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)
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Aman H
  • 1
  • 2
  • You never seem to ask the user for new `input(). Also you have to return `name_1` from the function. – tobias_k Oct 20 '20 at 10:32

4 Answers4

2

There are a few problems with your code:

  • you never ask the user for new input within the loop, instead just testing the first name again
  • you modify the (local) name_1 variable, but never return the value to the caller
  • you do not have to repeat all the conditions, and can use math to determine the number of tries remaining

You can try something like this:

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)
tobias_k
  • 81,265
  • 12
  • 120
  • 179
0

This is working for me, I think the way you had structured it meant the logic was flawed.

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()
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

Major changes:

  • put input() in while loop so you can get new input when the name is wrong, otherwise you only get the input once and the wrong name is checked by the if-statement 3 times
  • add return statement for allowed name so you can use the name later (such as print out or other functions)

You can find other minor changes in my code comment.

# 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)
adamkwm
  • 1,155
  • 2
  • 6
  • 18
-1

I have fixed your code problem.

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()
Prajjwal
  • 12
  • 2
  • Please don't add code as images - they're not searchable and they're not accessible for those who have trouble reading. – MatsLindh Oct 20 '20 at 11:01
  • iam new so when i tried putting code i was not able to put it . – Prajjwal Oct 20 '20 at 11:09
  • Edit it and leave it in your answer and tag me / just comment here, and I'll clean it up for you. Usually you can use the `code` button in the editor to get it tabbed in by four spaces automagically. – MatsLindh Oct 20 '20 at 11:16
  • I finally got it. I checked some info given by stack overflow. Thanks for helping me – Prajjwal Oct 20 '20 at 11:21
  • "Here is working code" is almost never a *useful* answer, even if the OP can copy and paste it as is. – Jongware Oct 20 '20 at 11:45
  • i was not trying to create advance code I just solve the problem he was facing. – Prajjwal Oct 20 '20 at 11:50