-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path08 Battle Master.py
More file actions
118 lines (86 loc) · 2.76 KB
/
Copy path08 Battle Master.py
File metadata and controls
118 lines (86 loc) · 2.76 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Battle Master
# Build the optimal army to defeat the dreaded orcs. Implement the use of TRY and EXCEPT statements
import random
import time
counter = 0
player1 = 0
player2 = 0
def player1_win():
print("Orcs win the battle")
global player1 # Generally speaking Global keyword is the worst
player1 += 1 # approach to interact with variable outside the
# function but its good to know how to do it
def player2_win(): # Better methods is using data structure like list or OOP (CLASS)
print("Humans win the battle")
global player2
player2 += 1
while True:
print("""The war between Orcs and Humans rage on forever
"You are the new commander, you have 100 men which you have to
allocate to the apprioate position to win the war """)
# The try and except block in Python is used to catch and handle exceptions.
while True:
knight = input("how many Knights ? : ")
try:
int(knight)
break
except:
print("This is not a number")
while True:
farmer = input("how many Farmers ? : ")
try:
int(farmer)
break
except:
print("This is not a number")
while True:
defender = input("how many Defenders ? : ")
try:
int(defender)
break
except:
print("This is not a number")
total = int(defender) + int(farmer) + int(knight)
if total > 100:
# F-string optional addition to show total allocated count:
print(f"You have allocated {total}, which is over 100")
else:
# Replaced comma separation and str() calls with an f-string:
print(
f"You have {defender} Defenders. You have {farmer} Farmers and {knight}"
" Knights"
)
break
while True:
while True:
counter += 1
print("The battle rages on and on ..")
time.sleep(0.5)
print("...")
time.sleep(0.5)
print("...")
time.sleep(0.5)
print("...")
if counter == 3:
break
print("A victor has been decided")
x = random.randint(1, 3)
time.sleep(x)
defender1 = int(defender) + random.randint(1, 15)
farmer1 = int(farmer) + random.randint(1, 10)
knight1 = int(knight) + random.randint(1, 15)
if defender1 >= 35 and farmer1 >= 15 and knight1 >= 50:
player2_win()
else:
player1_win()
# Replaced string concatenation (+) and str() calls with an f-string:
print(f"Orc victory: {player1} Human victory: {player2}")
counter = 0
if player1 == 5:
print("""The Orcs has won the war. You have been defeated !!""")
print("Tip : Priorities Knights then Defender and then Farmers ")
break
if player2 == 5:
print("The Humans has won the war, congratulations !!")
break
input("Press enter to exit")