Python Forum

Full Version: regarding dice game
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
how do I fix an error for this bit of code, I'm kind of new and look here for help. if you can help me I would appreciate it. the issue is it keeps saying player_1 isn't defined I don't know what to do. this function is for the actual main game for my program' any help is appreciated


def roll():
points = 0
die1 = random.randint(1,6)
die2 = random.randint(1,6)
dietotal = die1 + die2
points = points + dietotal

if dietotal % 2 == 0:
points = points + 10

else:
points = points - 5

if die1 == die2:
die3 = random.randint(1,6)
points = points +die3

return(points)

for i in range(1,5):
Player1Points += roll()
print('After this round ',player_1, 'you now have: ',Player1Points,' Points')
time.sleep(1)
Player2Points += roll()
print('After this round ',player_2, 'you now have: ',Player2Points,' Points')
time.sleep(1)

if Player1Points == Player2Points:
while Player1Tiebreaker == Player2Tiebreaker:
Player1Tiebreaker = random.randint(1,6)
Player2Tiebreaker = random.randint(1,6)

if Player1Tiebreaker > Player2Tiebreaker:
Player2Points = 0
elif Player2Tiebreaker > Player1Tiebreaker:
Player1Points = 0



if Player1Points>Player2Points:
Winner_Points = Player1Points
winner_User = player_1
winner = (Winner_Points, player_1)
elif Player2Points>Player1Points:
Winner_Points = Player2Points
winner = (Winner_Points, player_2)
winner_User = player_2

print('\nWell done, ', winner_User,' you won with ',Winner_Points,' Point\n\n')


leaderboard1 = "{}:{}".format(Winner_Points,winner_User)
with open ("Leaderboard.txt", "a") as file:
file.write("\n")
file.write(leaderboard1)


f = open('Leaderboard.txt', 'r')
leaderboard = [line.replace('\n','') for line in f.readlines()]
f.close()

leaderboard.sort(reverse=True)

with open('Leaderboard.txt', 'w') as f:
for item in leaderboard:
f.write("%s\n" % item)

print("TOP 5 SCORES:")
with open("Leaderboard.txt", "r") as f:
counter = 0
N = 1
for line in f:
head,sep,tail = line.partition(":")
print("Place No.{}\nUsername: {}Score: {}\n".format(N,tail,head))
N = N + 1
counter += 1
if counter == 5: break
You just forgot to initialize a few variables. Try adding these to your code :

Player1Points = 0
Player2Points = 0
player_1 = 'Foo'
player_2 = 'Bar'
This much works for me :

import random 
import time

def roll():
	points = 0
	die1 = random.randint(1,6)
	die2 = random.randint(1,6)
	dietotal = die1 + die2
	points = points + dietotal
	if dietotal % 2 == 0:
		points = points + 10
	else:
		points = points - 5
	if die1 == die2:
		die3 = random.randint(1,6)
		points = points +die3
	return(points)

Player1Points = 0
Player2Points = 0
player_1 = 'Foo'
player_2 = 'Bar'

for i in range(1,5):
	Player1Points += roll()
	print('After this round ',player_1, 'you now have: ',Player1Points,' Points')
	time.sleep(1)
	Player2Points += roll()
	print('After this round ',player_2, 'you now have: ',Player2Points,' Points')
	time.sleep(1)

if Player1Points == Player2Points:
	while Player1Tiebreaker == Player2Tiebreaker:
		Player1Tiebreaker = random.randint(1,6)
		Player2Tiebreaker = random.randint(1,6)
		if Player1Tiebreaker > Player2Tiebreaker:
			Player2Points = 0
		elif Player2Tiebreaker > Player1Tiebreaker:
			Player1Points = 0



if Player1Points>Player2Points:
	Winner_Points = Player1Points
	winner_User = player_1
	winner = (Winner_Points, player_1)
elif Player2Points>Player1Points:
	Winner_Points = Player2Points
	winner = (Winner_Points, player_2)
	winner_User = player_2

print('\nWell done, ', winner_User,' you won with ',Winner_Points,' Point\n\n')
thanks so much!
but i cant specify the players names because the user inputs it so how would I do that ? sorry my englsh isn't very good
(Feb-10-2021, 09:55 PM)hola123 Wrote: [ -> ]but i cant specify the players names because the user inputs it so how would I do that ? sorry my englsh isn't very good

player_1 = input ('Player one, what is your name? ')
player_2 = input ('Player two, what is your name? ')
thank you very much