Python Forum

Full Version: can't figure out what is wrong with this code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am relatively new to Python taking GCSE Computer Science at my school. This is for my programming project. I have to create a dice game that has two players roll two dice and add the scores to the respective player. If the two dice are equal, the code will roll a third die. If the total of the two (or three) dice is even, 10 will be added to the score. If it is odd, 5 will be taken away from the score. The problems I am having is that:
1. The score always displays as 0
2. I'm not sure how to add the score shown to the respective players' totals (p1total, p2total)
I have done some testing and still cannot figure out what I am doing wrong Huh . Suggestions would be appreciated.
import random
import time

roundnum = 0
p1total = 0
p2total = 0
p1wins = 0
p2wins = 0
score = 0

def diceroll():
    dice1 = (random.randrange (1,6))
    dice2 = (random.randrange (1,6))
    score = dice1 + dice2
    print("score debug: ", score)
    if score %2 == 0:
        score += 10
        print("score debug2: ", score)
        if dice1 == dice2:
            print("score debug3: ", score)
            dice3 = (random.randrange (1,6))
            score = score + dice3
    else:
        score = score - 5
        print("score debug4: ", score)
        if score < 0:
            print("score debug5: ", score)
            score = 0
            
    print("The first die shows", dice1)
    time.sleep(1)
    print("The second die shows", dice2)
    time.sleep(1)
    if dice1 == dice2:
        print("These two values are equal, so we will roll a third die.")
        time.sleep(1)
        print("The third die shows", dice3)

def suddendeath():
    time.sleep(1)
    global winner
    winner = 0
    print("Welcome to Sudden Death!")
    time.sleep(1)
    dice1 = (random.randrange (1,6))
    print("Player One scored", dice1)
    time.sleep(1)
    dice2 = (random.randrange (1,6))
    print("Player Two scored", dice2)
    time.sleep(1)
    if dice1 > dice2:
        winner = 1
        print("Player One wins!")
    elif dice2 > dice1:
        winner = 2
        print("Player Two wins!")
    else:
        print("The two scores are level! Sudden Death continues!")
        suddendeath()


for i in range (5):
    roundnum += 1
    print ("\nThis is Round", roundnum)

#Player One
    time.sleep(1)
    print("\nPlayer One, your go...")
    time.sleep(1)
    diceroll()
    print("\nPlayer One, you scored", score)
    p1total += score

#Player Two
    time.sleep(1)
    print("\nPlayer Two, your go...")
    time.sleep(1)
    diceroll()
    print("\nPlayer Two, you scored", score)
    p2total += score

#SuddenDeath
    if p1total == p2total:
        suddendeath()
Have you considered using the debugger to watch your program execute?
You're encountering an issue with scope. Score is always 0 because it is a global variable. On line 14, you add the value of dice1 and dice2 to a local variable called "score"; this is not the same as your global variable even though it has the same name. Instead, this score variable is local to the diceroll() function and only exists while that function is running.

To correct it, you can return the score in the diceroll() function and then replace your for loop with something like this:

for i in range (5):
    roundnum += 1
    print ("\nThis is Round", roundnum)

#Player One
    time.sleep(1)
    print("\nPlayer One, your go...")
    time.sleep(1)
    p1score = diceroll()
    print("\nPlayer One, you scored", p1score)
    p1total += p1score

#Player Two
    time.sleep(1)
    print("\nPlayer Two, your go...")
    time.sleep(1)
    p2score = diceroll()
    print("\nPlayer Two, you scored", p2score)
    p2total += p2score

#SuddenDeath
    if p1total == p2total:
        suddendeath()
There may be other issues as well; I stopped as soon as I saw this one.