-
Notifications
You must be signed in to change notification settings - Fork 1
/
Problem-1
40 lines (40 loc) · 1.62 KB
/
Problem-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import random
def get_computer_choice():
choices = ['rock', 'paper', 'scissors']
return random.choice(choices)
def get_user_choice():
user_choice = input("Choose any one {rock, paper, scissors}: ").lower()
while user_choice not in ['rock', 'paper', 'scissors']:
user_choice = input("Invalid choice. WARNING!!! \nPlease choose again [rock, paper, scissors]: ").lower()
return user_choice
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "The match is Drawn or, tie....."
if (user_choice == 'rock' and computer_choice == 'scissors') or \
(user_choice == 'scissors' and computer_choice == 'paper') or \
(user_choice == 'paper' and computer_choice == 'rock'):
return "You win!"
else:
return "You lose!"
def play_game():
history = []
while True:
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print(f"\nYour Choise: {user_choice}")
print(f"Computer Choise: {computer_choice}")
result = determine_winner(user_choice, computer_choice)
print(result)
history.append(result)
if len(history) > 3:
history.pop(0)
print("\nLast 3 game results:")
for i, res in enumerate(history, 1):
print(f"{i}: {res}")
play_again = input("\nDo you want to play again? (yes/no): ").lower()
while play_again not in ['yes', 'no']:
play_again = input("Invalid input. Please enter ('yes' or 'no'): ").lower()
if play_again == 'no':
break
if __name__ == "__main__":
play_game()