-2

I have tried to create the program to play "Rock, Paper, Scissors, Lizard & Spock", as I had done to create the simple game of "Rock, paper, Scissors", however I cannot with the prompts they asked me:

In addition, it should also improve the way the game interacts with the player: the number of rounds to play, which must be an odd number, will be requested from the user until a valid number is entered. Define a new function to make that request.

Could you help me please? this is the code i used:

import random

gestures = ['rock', 'paper', 'scissors', 'lizard', 'spock']

n_rounds = 0
cpu_score = 0
player_score = 0

import random

gestures = ['rock', 'paper', 'scissors', 'lizard', 'spock']

n_rounds = 0
cpu_score = 0
player_score = 0

def rounds_to_win(gestures):
    rounds_to_win = print("How many rounds you want to play? Introduce an odd number")
    while rounds_to_win %2 == 0:
        if rounds_to_win % 2 > 0:
            print("This isn't a valid option. Introduce an odd numbe")
        elif rounds_to_win %2 < 0:
            print("You chose", rounds_to_win)

def cpu_choice (gestures):
    return random.choice(gestures)

def player_choice(gestures):
    player_choice = input("Stone, paper, scissors, lizard or spock?. Please, type your choice.")
    
    while player_choice not in gestures:
        player_choice = input("That's not an option. Stone, paper, scissors, lizard or spock?")
        
        if player_choice == "stone":
            print("You choose stone")
        elif player_choice == "paper":
            print("You choose paper")
        elif player_choice == "scissors":
            print("You choose scissor")
        elif player_choice == "lizard":
            print("You choose lizard")
        elif player_choice == "spock":
            print("You choose Spock")
    
    return str(player_choice)

def choice(gestures):
    print("You chose:", player_choice)
    print("CPU chose:", cpu_choice)
    if player_choice == "stone" and cpu_choice == "stone":
        print("Stone vs stone. It's a tie")
        cpu_score += 0
        player_score += 0
        n_rounds += 1
    elif player_choice == "paper" and cpu_choice == "paper":
        print("Paper vs paper. It's a tie")
        cpu_choice += 0
        player_score += 0
        n_rounds += 1
    elif player_choice == "scissors" and cpu_choice == "scissors":
        print("Scissors vs scissors. It's a tie")
        cpu_score += 0
        player_score += 0
        n_rounds += 1
        
    elif player_choice == "stone" and cpu_choice == "paper":
        print("Stone vs paper. You loose")
        cpu_score += 0
        player_score += 2
        n_rounds += 1
    elif player_choice == "stone" and cpu_choice == "scissors":
        print("Stone vs scissors. You win")
        cpu_choice += 1
        player_score += 0
        n_rounds += 1
        
    elif player_choice == "paper" and cpu_choice == "stone":
        print("Paper vs stone. You win")
        cpu_score += 0
        player_score += 2
        n_rounds += 1
    elif player_choice == "paper" and cpu_choice == "scissors":
        print("Paper vs Scissors. You loose")
        cpu_choice += 1
        player_score += 0
        n_rounds += 1
    
    elif player_choice == "scissors" and cpu_choice == "paper":
        print("Scissors vs paper. You win")
        cpu_score += 0
        player_score += 2
        n_rounds += 1
    elif player_choice == "scissors" and cpu_choice == "stone":
        print("Scissors vs stone. You loose")
        cpu_choice += 1
        player_score += 0
        n_rounds += 1
        
def winner():
    print((player_score) - (cpu_score))
    
while n_rounds < 5:
    rounds_to_win(gestures)
    player_choice(gestures)
    cpu_choices = cpu_choice(gestures)
    winner()
    if player_score > cpu_score:
        print("You won")
    else:
        print("CPU won")
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Why not the opposite %2 != 0. If its not divisible by 2, then it must be an odd number. Also, if you're doing % 2>0, you don't need an elif %2<0, by typing else you're implying that. Delete the top half of your code also, it just repeats twice. Also, you have 3 functions but none are ever called. So your code is doing nothing – samman Jul 10 '20 at 05:25

1 Answers1

0

I presume this is what you were trying to make:

import random

gestures = ['rock', 'paper', 'scissors', 'lizard', 'spock']

def rounds_to_win(gestures):
    while True:
        rounds_to_win = input("How many rounds you want to play? Introduce an odd number")
        if int(rounds_to_win) %2 != 0:
            break
        else:
            print('please input odd number')
    return int(rounds_to_win)


def cpu_choice(gestures):
    return random.choice(gestures)

def player_choice(gestures):
    while True:
        players_choice = input("Stone, paper, scissors, lizard or spock?. Please, type your choice.")
        if players_choice not in gestures:
            print('input not valid')
        else:
            break
    return players_choice

def main_loop(gestures):
    player_score=0
    cpu_score=0
    rounds_played=0
    for i in range(rounds_to_win(gestures)):
        rounds_played+=1
        if player_choice(gestures) == cpu_choice(gestures):
            print('you win')
            player_score+=1
        else:
            print('you lose')
            cpu_score+=1
    print(f'Player score {player_score}\nCPU Score {cpu_score}\nRounds Played {rounds_played}')

main_loop(gestures)
samman
  • 559
  • 10
  • 29