-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject1.py
More file actions
34 lines (27 loc) ยท 1.1 KB
/
project1.py
File metadata and controls
34 lines (27 loc) ยท 1.1 KB
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
import random
# Computer randomly chooses one of Snake (1), Gun (0), Water (-1)
computer = random.choice([1, 0, -1])
# Ask user for their choice
youstr = input("Enter your choice (s for Snake, w for Water, g for Gun): ")
# Dictionary to convert user input to numeric values
youDict = {"s": 1, "w": -1, "g": 0}
# Reverse dictionary to convert numeric values back to names
reverseDict = {1: "snake", -1: "water", 0: "gun"}
# Validate user input
if youstr not in youDict:
print("Invalid input! Please choose 's', 'w', or 'g'.")
else:
# Convert user input into corresponding number
you = youDict[youstr]
# Show what each player chose
print(f"You chose {reverseDict[you]}")
print(f"Computer chose {reverseDict[computer]}")
# Determine result
if computer == you:
print("It's a draw!")
elif (you == 1 and computer == -1) or (you == 0 and computer == 1) or (you == -1 and computer == 0):
# All winning conditions for the user
print("You win!")
else:
# All other conditions mean the computer wins
print("You lose!")