Create your own
teaching resources

Sign up for a free account to try! It takes less than 20 seconds

Teacherbot 30 Apr, 13:42:07

import random

wins = 0 rounds = 0

def computer_choice(): choices = [‘rock’, ‘paper’, ‘scissors’] return random.choice(choices)

def user_choice(): choice = input(“Choose rock, paper, or scissors: “) while choice not in [‘rock’, ‘paper’, ‘scissors’]: choice = input(“Invalid choice. Choose rock, paper, or scissors: “) return choice

def determine_winner(computer, user): global wins if computer == user: print(“It’s a tie!”) elif (computer == ‘rock’ and user == ‘scissors’) or (computer == ‘paper’ and user == ‘rock’) or (computer == ‘scissors’ and user == ‘paper’): print(“Computer wins!”) else: print(“You win!”) wins += 1

def play_game(): global rounds for i in range(rounds): print(f”Round {i+1}:”) computer = computer_choice() user = user_choice() print(f”Computer chose {computer}.”) determine_winner(computer, user) print(f”Score: {wins}/{i+1}”) print()

def display_results(): print(f”Final score: {wins}/{rounds}”)

def main(): print(“Welcome to the Guessing Game!”) while True: print(“Choose a difficulty level:”) print(“1. Easy”) print(“2. Medium”) print(“3. Hard”) choice = input(“Enter your choice: “) if choice in [‘1’, ‘2’, ‘3’]: break else: print(“Invalid choice. Please try again.”) if choice == ‘1’: rounds = 3 print(“You have chosen Easy.”) elif choice == ‘2’: rounds = 5 print(“You have chosen Medium.”) else: rounds = 10 print(“You have chosen Hard.”) play_game() display_results() while True: play_again = input(“Would you like to play again? (y/n): “) if play_again.lower() == ‘y’: wins = 0 main() elif play_again.lower() == ‘n’: print(“Thanks for playing!”) break else: print(“Invalid choice. Please try again.”)

main()

You 30 Apr, 13:43:10

Make this simpler to understand

Teacherbot 30 Apr, 13:43:10

This is a program that plays a guessing game with the user. The computer will randomly choose between rock, paper, and scissors, and the user will also choose one of these options. The program will determine the winner and keep track of the number of wins.

The program has three levels of difficulty: Easy, Medium, and Hard. The number of rounds played will depend on the difficulty level chosen.

The program will ask the user to choose a difficulty level and will keep asking until a valid response is entered. Once a difficulty level is chosen, the program will display a message confirming the selected difficulty and set the number of rounds accordingly.

The program will then loop a number of times equal to the number of rounds. For each round, the computer will randomly choose between rock, paper, and scissors, and the user will be asked to choose as well. The program will determine the winner and display the results so far.

Once the user has completed the number of rounds, the program will display the final results. The program will then ask the user if they would like to play again and display the difficulty menu again.

You 30 Apr, 13:43:51

Make this more complex

Teacherbot 30 Apr, 13:43:51

Here’s a more complex version of the program that includes additional features:

  1. The program keeps track of the user’s win/loss record and displays it at the end of each game.
  2. The program allows the user to choose between different game modes, including classic rock-paper-scissors, rock-paper-scissors-lizard-spock, and a custom mode where the user can define their own set of choices.
  3. The program allows the user to play against the computer or against another player.
  4. The program includes error handling to prevent the user from entering invalid input.

``` import random

class Game: def init(self, mode): self.mode = mode self.choices = self.get_choices() self.players = self.get_players() self.scores = {player: 0 for player in self.players}

def get_choices(self):
    if self.mode == "classic":
        return ["rock", "paper", "scissors"]
    elif self.mode == "lizard-spock":
        return ["rock", "paper", "scissors", "lizard", "spock"]
    elif self.mode == "custom":
        choices = input("Enter your choices, separated by commas: ")
        return [choice.strip() for choice in choices.split(",")]

def get_players(self):
    while True:
        num_players = input("Enter the number of players (1 or 2): ")
        if num_players in ["1", "2"]:
            break
        else:
            print("Invalid input. Please enter 1 or 2.")
    if num_players == "1":
        return ["player", "computer"]
    else:
        return ["player 1", "player 2"]

def get_player_choice(self, player):
    while True:
        choice = input(f"{player}, choose {self.mode}: ")
        if choice in self.choices:
            return choice
        else:
            print("Invalid input. Please choose one of the available options.")

def get_computer_choice(self):
    return random.choice(self.choices)

def determine_winner(self, choices):
    if choices[0] == choices[1]:
        print("It's a tie!")
    elif (choices[0] == "rock" and choices[1] == "scissors") or (choices[0] == "paper" and choices[1] == "rock") or (choices[0] == "scissors" and choices[1] == "paper"):
        print(f"{self.players[0]} wins!")
        self.scores[self.players[0]] += 1
    else:
        print(f"{self.players[1]} wins!")
        self.scores[self.players[1]] += 1

def play_game(self):
    for i in range(len(self.players)):
        choices = []
        if self.players[i] == "computer":
            choices.append(self.get_computer_choice())
        else:
            choices.append(self.get_player_choice(self.players[i]))
        print(f"{self.players[i]} chose {choices[0]}.")
        if len(self.players) == 2:
            choices.append(self.get_player_choice(self.players[(i+1)%2]))
            print(f"{self.players[(i+1)%2]} chose {choices[1]}.")
        else:
            choices.append(self.get_computer_choice())
            print(f"The computer chose {choices[1]}.")
        self.determine_winner(choices)
        print(f"Score: {self.scores[self.players[0]]} - {self.scores[self.players[1]]}")
        print()

def display_results(self):
    print("Final score:")
    for player in self.players:
        print(f"{player}: {self.scores[player]}")
    if self.scores[self.players[0]] > self.scores[self.players[1]]:
        print(f"{self.players[0]} wins!")
    elif self.scores[self.players[0]] < self.scores[self.players[1]]:
        print(f"{self.players[1]} wins!")
    else:
        print("It's a tie!")

def main(): print(“Welcome to the Rock-Paper-Scissors Game!”) while True: mode = input(“Choose a game mode (classic, lizard-spock, custom): “) if mode in [“classic”, “lizard-spock”, “custom”]: break else: print(“Invalid input. Please choose one of the available options.”) game = Game(mode) while True: game.play_game() game.display_results() while True: play_again = input(“Would you like to play again? (y/n): “) if play_again.lower() == “y”: game.scores = {player: 0 for player in game.players} break elif play_again.lower() == “n”: print(“Thanks for playing!”) return else: print(“Invalid input. Please enter y or n.”)

if name == “main”: main() ```