Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding error- help
#1
Hello, I'm new to the forum! I'm having an issue with my double dice game code, I keep getting a syntax error. Help and any advice would be appreciated!

import random 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 rounds=1 while rounds !=4: print("Round"+str(rounds)) player_1=dice_roll() player_12=dice_roll() player_2=dice_roll() player_22=dice_roll() print("Player one rolls a: " + str(player_1 + player_12)) print("Player two rolls a: "+ str(player_2+ player_22)) if player_1 +player_12 ==player_2+player_12: print("It's a tie!") elif player_1+ player_12>player_2 + player_22: print("Player 1 wins!") totalpoints1 = totalpoints2 + 1 else: print("Player 2 Wins!") totalpoints2 = totalpoints1 + 1 rounds=rounds+1 if Points1 + Points12 ==Points2 + Points22: print("It's a tie!") elif Points1 + Points12 >Points2 + Points22: print("player 1 wins!") else: print("Player 2 Wins!") def dice_roll(): diceRoll=random.randint(1,6) return diceRoll
Reply
#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
#3
Hi! Thank you for the reply & sorry for the late reply, but when I modified dice_roll function, I got an error:

File "C:/Users/FatherYeals/PycharmProjects/DiceGame2/Dice Game1.py", line 14, in <module>
player1_roll = dice_roll() + dice_roll()
TypeError: dice_roll() missing 1 required positional argument: 'player_name'
Reply
#4
Guess that was a little confusing. To use the enhanced dice_roll function you would have to modify the function calls to provide 'Player 1' or 'Player 2'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Coding error. xflyerwdavis 2 1,600 Oct-07-2023, 07:08 PM
Last Post: deanhystad
  Coding error. Can't open directory EddieG 6 3,178 Jul-13-2023, 06:47 PM
Last Post: deanhystad
  Coding Error EddieG 2 1,574 Jul-09-2023, 02:59 AM
Last Post: EddieG
  python coding error isntitzee 1 3,280 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 2,642 Sep-28-2020, 07:57 AM
Last Post: buran
  coding error iro a menu Rollo 2 3,254 Sep-27-2020, 04:17 PM
Last Post: deanhystad
  Adafruit LIS3DH coding error FitDad73 1 3,487 Aug-30-2020, 01:37 AM
Last Post: bowlofred
  name undefined Coding Error Ewilliam51 2 3,322 Feb-06-2020, 12:19 AM
Last Post: Ewilliam51
  coding error bilawal 11 9,145 Dec-07-2019, 05:23 PM
Last Post: DreamingInsanity
  Pyhton Coding Help Needed (very small error) friduk 2 3,722 Oct-23-2019, 08:41 AM
Last Post: Gribouillis

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.