Reputation: 29
I'm learning python and i'm making a 2 player dice-rolling game but i can't seem to allow the players to roll or play another round. This is the code that i've written. Id appreciate it if someone could help me correct the codes. Thankss!
import random
import time
player_a = input("Player1, please type your name:")
time.sleep(1)
player_b = input("Player2, please type your name:")
time.sleep(1)
print("Welcome to Dice Roller", player_a, "and", player_b)
score_a = 0
score_b = 0
roll_a = False
roll_b = False
while not (roll_a and roll_b):
ans_a = input("Type roll to roll the dice:")
if ans_a == "roll":
roll_a = (random.randint(1, 6))
time.sleep(1)
print(player_a, "has rolled", roll_a)
else:
print("Please check you spelling")
time.sleep(2)
ans_b = input("Your turn to roll, type roll to roll the dice:")
if ans_b == "roll":
roll_b = (random.randint(1, 6))
time.sleep(1)
print(player_b, "has rolled", roll_b)
if roll_a == roll_b:
time.sleep(2)
print("Tie")
break
roll_a = False
roll_b = False
if roll_a > roll_b:
score_a = roll_a - roll_b
print(player_a, "has scored", score_a, "points")
break
roll_a = False
roll_b = False
if roll_b > roll_a:
score_b = roll_b - roll_a
print(player_b, "has scored", score_b, "points")
break
roll_a = False
roll_b = False
Upvotes: 0
Views: 163
Reputation: 778
The break
statements immediately quits the while loop so the code after the first break
is never run. See the documentation here for more info: https://www.tutorialspoint.com/python/python_break_statement.htm
There are also several other issues with your code which have not shown up yet (because that section of code never gets run):
In the following section roll_a
and roll_b
get overwriten with False
. This means the comparison in the if-statment will not work. You are also overwritting score_a
, where I would assume you would want to add to it (use the +=
operator instead of =
).
roll_a = False
roll_b = False
if roll_a > roll_b:
score_a = roll_a - roll_b
print(player_a, "has scored", score_a, "points")
I would suggest something like the following (note I have not tested this!):
play_another_round = True
while play_another_round:
ans_a = input("Type roll to roll the dice:")
if ans_a == "roll":
roll_a = (random.randint(1, 6))
time.sleep(1)
print(player_a, "has rolled", roll_a)
else:
print("Please check you spelling")
time.sleep(2)
ans_b = input("Your turn to roll, type roll to roll the dice:")
if ans_b == "roll":
roll_b = (random.randint(1, 6))
time.sleep(1)
print(player_b, "has rolled", roll_b)
if roll_a == roll_b:
print("Tie")
elif roll_a > roll_b:
score_a += roll_a - roll_b
print(player_a, "has scored", score_a, "points")
elif roll_b > roll_a:
score_b += roll_b - roll_a
print(player_b, "has scored", score_b, "points")
continue_game_check = input("Do you want to play another round (Yes/No)?")
play_another_round = continue_game_check == "Yes"
print("The game has ended")
print(player_a, "has scored", score_a, "points")
print(player_b, "has scored", score_b, "points")
Upvotes: 0
Reputation: 168863
You might want to refactor your game to a function that requests the user(s) to type "roll" and returns the roll result, like below.
The main while True
loop will never end (though you could make it something like for round in range(5):
to play 5 rounds), and the request_roll()
loop will be broken out of with the return
.
import random
import time
def request_roll(name):
while True:
ans = input(f"{name}, type roll to roll the dice:").lower()
if ans == "roll":
roll = random.randint(1, 6)
time.sleep(1)
print(name, "has rolled", roll)
return roll
print("Please check your spelling")
time.sleep(2)
player_a = input("Player1, please type your name:")
player_b = input("Player2, please type your name:")
print("Welcome to Dice Roller", player_a, "and", player_b)
while True:
roll_a = request_roll(player_a)
roll_b = request_roll(player_b)
if roll_a > roll_b:
score_a = roll_a - roll_b
print(player_a, "has scored", score_a, "points")
elif roll_b > roll_a:
score_b = roll_b - roll_a
print(player_b, "has scored", score_b, "points")
else:
print("It's a draw")
Upvotes: 2