-
Notifications
You must be signed in to change notification settings - Fork 263
/
Python.py
80 lines (60 loc) · 1.68 KB
/
Python.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Import random module
import random
print('Snake - Water - Gun')
# Input no. of rounds
n = int(input('Enter number of rounds: '))
# List containing Snake(s), Water(w), Gun(g)
options = ['s', 'w', 'g']
# Round numbers
rounds = 1
# Count of computer wins
comp_win = 0
# Count of player wins
user_win = 0
# There will be n rounds of game
while rounds <= n:
# Display round
print(f"Round :{rounds}\nSnake - 's'\nWater - 'w'\nGun - 'g'")
# Exception handling
try:
player = input("Choose your option: ")
except EOFError as e:
print(e)
# Control of bad inputs
if player != 's' and player != 'w' and player != 'g':
print("Invalid input, try again\n")
continue
# random.choice() will randomly choose
# item from list- options
computer = random.choice(options)
# Conditions based on the game rule
if computer == 's':
if player == 'w':
comp_win += 1
elif player == 'g':
user_win += 1
elif computer == 'w':
if player == 'g':
comp_win += 1
elif player == 's':
user_win += 1
elif computer == 'g':
if player == 's':
comp_win += 1
elif player == 'w':
user_win += 1
# Announce winner of every round
if user_win > comp_win:
print(f"You Won round {rounds}\n")
elif comp_win > user_win:
print(f"Computer Won round {rounds}\n")
else:
print("Draw!!\n")
rounds += 1
# Final winner based on the number of wons
if user_win > comp_win:
print("Congratulations!! You Won")
elif comp_win > user_win:
print("You lose!!")
else:
print("Match Draw!!")