From 428d6aa4705e3c5e06f967285942c1abfef07179 Mon Sep 17 00:00:00 2001 From: DotNetAbhishek <50742393+ashriv2020@users.noreply.github.com> Date: Sat, 1 Jun 2024 13:11:02 +0530 Subject: [PATCH 1/4] chore: Add rock-paper-scissors game logic and play_again functionality --- app.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/app.py b/app.py index e69de29..705c718 100644 --- a/app.py +++ b/app.py @@ -0,0 +1,40 @@ +# writ 'Hello Wrld' to the console +import random + + +print('Hello World') +# Create a method to play the game +def play_game(): + # Create a list with three options rock, paper, or scissors + options = ['rock', 'paper', 'scissors'] + # Create a variable to store the user's choice + user_choice = input('Enter rock, paper, or scissors: ') + # Create a variable to store the computer's choice + computer_choice = random.choice(options) + # Print the user's choice + print('User choice:', user_choice) + # Print the computer's choice + print('Computer choice:', computer_choice) + # Compare the user's choice to the computer's choice + if user_choice == computer_choice: + print('Tie!') + elif user_choice == 'rock' and computer_choice == 'scissors': + print('You win!') + elif user_choice == 'paper' and computer_choice == 'rock': + print('You win!') + elif user_choice == 'scissors' and computer_choice == 'paper': + print('You win!') + else: + print('You lose!') + +# Call the play_game method +play_game() +# ask to play again +play_again = input('Do you want to play again? (yes or no): ') +if play_again == 'yes' or play_again == 'y': + play_game() +else: + print('Thanks for playing!') + +# print message to the console +print('Goodbye!') \ No newline at end of file From f7cca1783187d699f9c14a5a5c6e534988dc762f Mon Sep 17 00:00:00 2001 From: DotNetAbhishek <50742393+ashriv2020@users.noreply.github.com> Date: Sat, 1 Jun 2024 13:18:24 +0530 Subject: [PATCH 2/4] chore: Refactor play_again functionality in app.py to play until user wants --- app.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 705c718..2831453 100644 --- a/app.py +++ b/app.py @@ -31,10 +31,10 @@ def play_game(): play_game() # ask to play again play_again = input('Do you want to play again? (yes or no): ') -if play_again == 'yes' or play_again == 'y': +while play_again.lower() == 'yes' or play_again.lower() == 'y': play_game() -else: - print('Thanks for playing!') + play_again = input('Do you want to play again? (yes or no): ') +print('Thanks for playing!') # print message to the console print('Goodbye!') \ No newline at end of file From 50ed528842d2324251e164de34ef6e0099203837 Mon Sep 17 00:00:00 2001 From: DotNetAbhishek <50742393+ashriv2020@users.noreply.github.com> Date: Sat, 1 Jun 2024 13:24:14 +0530 Subject: [PATCH 3/4] chore: Refactor play_again to capture number of rounds played --- app.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 2831453..ea155ae 100644 --- a/app.py +++ b/app.py @@ -27,14 +27,20 @@ def play_game(): else: print('You lose!') + +# create a variable to store the play again count +play_again_count = 1 + # Call the play_game method play_game() # ask to play again play_again = input('Do you want to play again? (yes or no): ') while play_again.lower() == 'yes' or play_again.lower() == 'y': + # increase the play again count + play_again_count += 1 play_game() play_again = input('Do you want to play again? (yes or no): ') -print('Thanks for playing!') +print('Thanks for playing! You played', play_again_count, 'times.') # print message to the console print('Goodbye!') \ No newline at end of file From 1a1a06ebe211a1511bf107ee5470f06d560e263b Mon Sep 17 00:00:00 2001 From: DotNetAbhishek <50742393+ashriv2020@users.noreply.github.com> Date: Sun, 2 Jun 2024 12:45:18 +0530 Subject: [PATCH 4/4] chore: Refactor play_game method and add user win count tracking --- app.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index ea155ae..bad8e8d 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,19 @@ # writ 'Hello Wrld' to the console import random - -print('Hello World') +user_win_count = 0 # Create a method to play the game def play_game(): + global user_win_count #to use global variable into the method define it as global + print('Welcome to Rock, Paper, Scissors!', 'User Score:', user_win_count) # Create a list with three options rock, paper, or scissors options = ['rock', 'paper', 'scissors'] # Create a variable to store the user's choice user_choice = input('Enter rock, paper, or scissors: ') + # Check if the user's choice is valid + while user_choice.lower() not in options: + print('Invalid choice. Please try again.') + user_choice = input('Enter rock, paper, or scissors: ') # Create a variable to store the computer's choice computer_choice = random.choice(options) # Print the user's choice @@ -20,10 +25,13 @@ def play_game(): print('Tie!') elif user_choice == 'rock' and computer_choice == 'scissors': print('You win!') + user_win_count += 1 elif user_choice == 'paper' and computer_choice == 'rock': print('You win!') + user_win_count += 1 elif user_choice == 'scissors' and computer_choice == 'paper': print('You win!') + user_win_count += 1 else: print('You lose!') @@ -31,6 +39,7 @@ def play_game(): # create a variable to store the play again count play_again_count = 1 + # Call the play_game method play_game() # ask to play again @@ -40,7 +49,7 @@ def play_game(): play_again_count += 1 play_game() play_again = input('Do you want to play again? (yes or no): ') -print('Thanks for playing! You played', play_again_count, 'times.') +print('Thanks for playing! You played', play_again_count, 'times. Won', user_win_count, 'times.') # print message to the console print('Goodbye!') \ No newline at end of file