Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding error- help
#2
There's a few problems here. Let's start with Dice(). What do you think this is doing? What is it's purpose?
I think you mean for Dice to be a structure that holds information for your dice game. Look what happens when I run this program:
def Dice():
    player_1=0
    Points1=0
    player_2=0
    Points2=0
    player_12=0
    player_22=0
    Points12=0
    Points22=0
    totalpoints1=Points1 + Points12
    totalpoints2=Points2 + Points22

print('Dice is a', type(Dice))
print('Dice knows about', Dice.__dict__)
print(totalpoints1)
Output:
Dice is a <class 'function'> Dice knows about {} Traceback (most recent call last): File "C:/Users/djhys/Documents/Python/schema/dsp/regex.py", line 17, in <module> print(totalpoints1) NameError: name 'totalpoints1' is not defined
Surprise! Dice is a function. The code above creates a function that assigns a bunch of local variables and returns None. And those local variables are only visible from inside the function while it is running. When I tried to print "totalpoints1" I got an error because there is no such thing outside the Dice function.

For a simple program like this I would forget about any kind of data structure until I got all the parts working. Something like this
import random
 
def dice_roll():
    return random.randint(1,6)

player1_points = 0
player2_points = 0

for rnd in range(1, 5):
    player1_roll = dice_roll() + dice_roll()
    player2_roll = dice_roll() + dice_roll()

    print('Round', rnd)
    if player1_roll == player2_roll:
        print("It's a tie!")
    elif player1_roll > player2_roll:
        print("Player 1 wins the roll!")
        player1_points += 1
    else:
        print("Player 2 wins the roll!")
        player2_points += 1

print('Game over')
if player1_points == player2_points:
    print("It's a tie!")
elif player1_points > player2_points:
    print("player 1 wins the game!")
else:
    print("Player 2 Wins the game!")
Output:
Round 1 Player 1 wins the roll! Round 2 Player 1 wins the roll! Round 3 Player 1 wins the roll! Round 4 It's a tie! Game over player 1 wins the game!
I think the game is a little sterile. To give it some flash I change the dice rolling to report which die were cast. To do this I modified the dice_roll function.
def dice_roll(player_name):
    die1 = random.randint(1,6)
    die2 = random.randint(1,6)
    print(player_name, 'rolls', die1, 'and', die2)
    return die1 + die2
See how this adds drama.
Output:
Round 1 Player 1 rolls 2 and 4 Player 2 rolls 6 and 6 Player 2 wins! Round 2 Player 1 rolls 4 and 6 Player 2 rolls 6 and 3 Player 1 wins! Round 3 Player 1 rolls 6 and 5 Player 2 rolls 5 and 3 Player 1 wins! Round 4 Player 1 rolls 4 and 6 Player 2 rolls 4 and 6 It's a tie! Game over player 1 wins the game, 2 to 1
Reply


Messages In This Thread
Coding error- help - by FatherYeals - Mar-08-2020, 10:51 PM
RE: Coding error- help - by deanhystad - Mar-09-2020, 12:29 AM
RE: Coding error- help - by FatherYeals - Mar-27-2020, 01:11 PM
RE: Coding error- help - by deanhystad - Mar-27-2020, 02:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Coding error. xflyerwdavis 2 642 Oct-07-2023, 07:08 PM
Last Post: deanhystad
  Coding error. Can't open directory EddieG 6 1,430 Jul-13-2023, 06:47 PM
Last Post: deanhystad
  Coding Error EddieG 2 652 Jul-09-2023, 02:59 AM
Last Post: EddieG
  python coding error isntitzee 1 2,301 Oct-17-2020, 06:30 AM
Last Post: buran
  Coding error- Not sure where I have put error markers against the code that is wrong Username9 1 1,835 Sep-28-2020, 07:57 AM
Last Post: buran
  coding error iro a menu Rollo 2 2,225 Sep-27-2020, 04:17 PM
Last Post: deanhystad
  Adafruit LIS3DH coding error FitDad73 1 2,121 Aug-30-2020, 01:37 AM
Last Post: bowlofred
  name undefined Coding Error Ewilliam51 2 2,256 Feb-06-2020, 12:19 AM
Last Post: Ewilliam51
  coding error bilawal 11 5,820 Dec-07-2019, 05:23 PM
Last Post: DreamingInsanity
  Pyhton Coding Help Needed (very small error) friduk 2 2,582 Oct-23-2019, 08:41 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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