Python Forum
can't figure out what is wrong with this code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can't figure out what is wrong with this code
#1
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()
Reply
#2
Have you considered using the debugger to watch your program execute?
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 480 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 849 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,555 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,249 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,780 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,536 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Not able to figure out what is wrong with argparse radioactive9 31 8,475 Mar-16-2022, 07:50 PM
Last Post: deanhystad
  Wrong code in Python exercise MaartenRo 2 1,526 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,630 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,497 Jun-09-2021, 10:01 AM
Last Post: topfox

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020