Python Forum
Selection not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selection not working
#1
My isuue for code below is , my code stop working after validate . This is a game with two players and the computer , the score has to save by player name , the game will be 3 rounds. I would like to know why my selection for the menu is not working. Thank you


from random import randint
Playernames=[]

 

def validate():

    while True:

        Player1 = input("what is the name of the first player : ")

        Player2 = input("what is the name of the second player : ")

        Playernames.append(Player1)

        Playernames.append(Player2)

        print(Player1,'and',Player2)

        if len(Player1 and Player2) < 5:

            print("Not valid ,please make sure your name is at least 5 letters")

        elif len(Player1 and Player2 ) > 20:

            print("Make sure your name has less than 20 letters")

 

        else:

 

            break

validate()

while True:
    print("1.Play Game")
    print("2.Show Game Rules")
    print("3.Show Statistics")
    print("4.Exit")
    selection=input('Please enter a selection:')
    if selection == '1':
       print('player1' or 'player2')
    Play = eval(input('How many games of Rock-Paper-Scissors do you want to play?:'))
    while (1):
       if (Play % 2 == 0):  # to check that number is not even
        Play = int(input('Please enter an odd number of games.:'))
       else:
        break
choice = ["Rock", "Paper", "Scissors" ,"Saw"]
computer=choice[randint(0,3)]
player1==false
player2==false
while player1 and player2==false:
    player1_win = 0# to count number of wins by user
    Player2_win = 0 # to count number of wins by user
    comp_win = 0  # to count number of wins by computer
    i = 0
    player1 = input("Rock, Paper, Scissors, Saw?")
    if player1 == computer:
        print("Tie!")
    elif player1 == "Rock":
        if computer == "Paper":
            print("You lose!", computer, "covers", player1)
        else:
            print("You win!", player1, "smashes", computer)
    elif player1 == "Paper":
        if computer == "Scissors":
            print("You lose!", computer, "cut", player1)
        else:
            print("You win!", player1, "covers", computer)
    elif player1 == "Scissors":
        if computer == "Rock":
            print("You lose...", computer, "smashes", player1)
        else:
            print("You win!", player1, "cut", computer)
            
    player2 = input("Rock, Paper, Scissors,Saw?")
    if player2 == computer:
        print("Tie!")
    elif player2 == "Rock":
        if computer == "Paper":
            print("You lose!", computer, "covers", player2)
        else:
            print("You win!", player2, "smashes", computer)
    elif player == "Paper":
        if computer == "Scissors":
            print("You lose!", computer, "cut", player2)
        else:
            print("You win!", player2, "covers", computer)
    elif player == "Scissors":
        if computer == "Rock":
            print("You lose...", computer, "smashes", player2)
        else:
            print("You win!", player2, "cut", computer)
        
Reply
#2
One thing that stands out immediately is that you are checking length, etc of player names after you have already stored them in the list, so your indexes on the Playernames list will be off the very first time you will have a length error.
Reply
#3
I actually do not have any issue before validate it works fine
Reply
#4
Actually it does not work properly:
If you add a few print statements, you will see that the list has the bad entries stored in as well as those accepted
Playernames=[]


def validate():
     while True:
        Player1 = input("what is the name of the first player : ")
        Player2 = input("what is the name of the second player : ")
        Playernames.append(Player1)
        Playernames.append(Player2)
        print(Player1,'and',Player2)
        if len(Player1 and Player2) < 5:
            print("Not valid ,please make sure your name is at least 5 letters")
        elif len(Player1 and Player2 ) > 20:
            print("Make sure your name has less than 20 letters")
        else:
            break
        print(f'Playernames: {Playernames}')

if __name__ == '__main__':
    validate()
    print(f'After all Playernames: {Playernames}')
output:
Output:
what is the name of the first player : zig what is the name of the second player : pat zig and pat Not valid ,please make sure your name is at least 5 letters Playernames: ['zig', 'pat'] what is the name of the first player : harry what is the name of the second player : zingo harry and zingo After all Playernames: ['zig', 'pat', 'harry', 'zingo']
Reply
#5
Thank you but I have another issue, it say player name not define on line 51 , I need to prompt player name and also I dont know how to make player turn.
from random import randint
Playernames=[]

def validate():
     while True:
        Player1 = input("what is the name of the first player : ")
        Player2 = input("what is the name of the second player : ")
        Playernames.append(Player1)
        Playernames.append(Player2)
        print(Player1,'and',Player2)
        if len(Player1 and Player2) < 5:
            print("Not valid ,please make sure your name is at least 5 letters")
        elif len(Player1 and Player2 ) > 20:
            print("Make sure your name has less than 20 letters")
        else:
            break
        print(f'Playernames: {Playernames}')
 
if __name__ == '__main__':
    validate()
while True:
    print("1.Play Game")
    print("2.Show Game Rules")
    print("3.Show Statistics")
    print("4.Exit")
    selection=input('Please enter a selection:')
    if selection == '1':
        Play = eval(input('How many games of Rock-Paper-Scissors do you want to play?:'))
        while (1):
            if (Play % 2 == 0):  # to check that number is not even
                Play = int(input('Please enter an odd number of games.:'))
            else:
                break

        options = ["Rock", "Paper", "Scissors" ,"Saw"]
        player1_win = 0  # to count number of wins by user
        comp_win = 0  # to count number of wins by computer
        i = 1

        while (Play):  # runs the while loop only number of games ti
            print("Game ", i)
            print("You can pick")
            print("R-Rock")
            print("P-Paper")
            print("S-Scissors")
            choice = input().strip().upper()
            if choice == 'R':
                computer = 'P'
                print("The computer picked Paper." + player1 +" " "Lose.")
                comp_win += 1
            elif choice == 'P':
                computer = 'S'
                print("The computer picked Paper." + player1 +" " "Lose.")
                comp_win += 1
            elif choice == 'S':
                computer = 'R'
                print("The computer picked Paper." + player1 +" " "Lose.")
                comp_win += 1
            else:
                player1_win += 1
            i += 1
            Play -= 1
            # strategy is to check what user has input and accordingly assign the value to
            # computer such that computer's choice defy the user choice. To fullfill this, I have chosen if else statement.
            # Since there are only 3 choices to compare, if else is a good mean to do that.
        print("You won ", player1_win, " games.")
        print("Computer won ", comp_win, " games.")
        if (comp_win > player1_win):
            print("Computer Wins!")
        else:
            print("You win!")
Reply
#6
Please show complete unmodified error traceback as it includes important information.
Reply
#7
Please see below

Traceback (most recent call last):
File "/Users/riphardnicolas/Documents/test6.py", line 50, in <module>
print("The computer picked Paper." + player1 +" " "Lose.")
NameError: name 'player1' is not defined
Reply
#8
Python is case-sensitive, meaning upper case letters are different from lower case letters. And I just became a master of silly walks. Walk this way.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020