# Calculating the percentage on Python 2.7

An attack with magnitude M will be successful with a chance of (100-M)%. That is, higher magnitude means higher risk of missing it. For instance, if M is 30, the chance of succeeding would be 70%, whereas, if M is 10, the chance of succeeding would be 90%. If an attack with magnitude M is successful, the attacked hero's health points will decrease by M. If the user writes more than 50 or less than 1, the game should warn the player, and re-ask for another attack magnitude as below.

import random

# ---

char1 = raw_input("Player One: ")

while char1 == "":

char2 = raw_input("Player two: ")
while char2 == char1:
char2 = raw_input(char1 + " name is taken, please choose another name: ")
while char2 == "":
print char1, "and", char2, "welcome to the game."

# ---

health1 = 50
health2 = 50

print char1, '|' * health1
print char2, '|' * health2

toss = random.randint(0, 1)

if toss == 0:
print char1, "will start the game"
else:
print char2, "will start the game"

# ---

while health1 > 0 and health2 > 0:
if toss == 0:
n = input(char1 + " select n force: ")
health2 -= n
print char2, '|' * health2 + char1,'|' * health1
toss = 1 # change player
else:
n = input(char2 + " select n force: ")
health1 -= n
print char1, '|' * health1 + char2,'|' * health2
toss = 0 # change player

# ---

if health1 > 0:
print char2, 'wins'
else:
print char1, 'wins'

• any hatred or hidden revenge feeling for use of class and functions? Commented Dec 19, 2017 at 5:16
• They must still be on the first learning steps of python. I had that kind of code when I was starting lol Commented Dec 20, 2017 at 4:30

I take it from your code the game works like this:

1. Each player enters their names. The names can't be blank nor the same with each other.
2. A coin is tossed to determine the turn order.
3. The turn player decides the amount of damage (M) he wants to deal against the other player. The chance of dealing damage is calculated by (100 - M).

With that, I have this code:

player1, player2 = input_player_names()
print("Welcome to the game {} and {}".format(player1, player2))
print("Tossing coin... Heads for {}; tails for {}".format(player1, player2))
turn_order = determine_turn_order([player1, player2])
print(turn_order[0], "plays first")
play(turn_order)


And the respective implementations:

def input_player_names():
player1 = ""
player2 = ""
while player1 == "":
while player2 == "" or player2 == player1:
return player1, player2

def determine_turn_order(player_list):
turn_order = []
toss = random.randint(0, 1)
turn_order.append(player_list[toss])
turn_order.append(player_list[toss + 1 % 2])
return turn_order

def play(turn_order):
player_health_points_list = [50, 50]
turn_number = 0
while there_are_no_winners_yet(player_health_points_list):
turn_player = turn_order[turn_number % 2]
player_is_sure = False
chance = 100
damage_to_deal = 0
while not player_is_sure:
damage_to_deal = input(turn_player + " enter damage you want to deal: ")
damage_to_deal = int(damage_to_deal)
chance = chance - damage_to_deal
if chance < 50:
player_is_sure = bool(input("You have only a {}% chance of dealing damage. Are you sure you want to push through? Input 1 for yes; 0  for no.".format(chance)))
else:
player_is_sure = True

# Here's how I'd calculate determine whether the attack hits or not
if chance > random.randint(1, 100):
player_health_points_list[(turn_number + 1) % 2] -= damage_to_deal
turn_number = turn_number + 1
print(turn_order[0], '|' * player_health_points_list[0])
print(turn_order[1], '|' * player_health_points_list[1])
declare_winner(turn_order, player_health_points_list)

def there_are_no_winners_yet(player_health_points_list):
return player_health_points_list[0] > 0 and player_health_points_list[1] > 0

def declare_winner(turn_order, player_health_points_list):
print("The winner is {}".format(turn_order[0] if player_health_points_list[0] > player_health_points_list[1] else turn_order[1]))

• please can you please check it again is not running. btw i am coding on python 2.7 Commented Dec 19, 2017 at 4:51
• Fixed it. Added a missing parenthesis. Commented Dec 20, 2017 at 4:27