Coding error- help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Coding error- help (/thread-24883.html) |
Coding error- help - FatherYeals - Mar-08-2020 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 RE: Coding error- help - deanhystad - Mar-09-2020 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) 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!") 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 + die2See how this adds drama.
RE: Coding error- help - FatherYeals - Mar-27-2020 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' RE: Coding error- help - deanhystad - Mar-27-2020 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' |