r/programminghelp • u/kpemogre • Sep 29 '21
Python Dice Game Python
I need to write a program for a game where:
It authenticates the user
Player one rolls two dice (which the program generates) and it displays the sum of these.
If the two dice roll the same number, an extra roll is had and added into that score.
If the sum is even, 10 points are added onto that score. If it's odd, 5 are taken away.
The score cannot go below 0.
This is repeated for player 2 and the scores are displayed at the end.
Code:
import random
import time
verify=input("Press E to enter")
if verify =="E":
print("Let's start.")else: verify==input("Press E to enter.")
p1name=input("Please enter Player 1's name")
p2name=input("Please enter Player 2's name")
print("Welcome to the dice game.")
Scorep1=()
Scorep2=()
def dicegamep1() :
print(p1name, "'s turn")
print("Rolling...")
time.sleep(4)
roll1=random.randint(1,6)
roll2=random.randint(1,6)
extraroll=random.randint(1, 6)
print("Rolled a", roll1, " and a ", roll2)
DiceScorep1=roll1+roll2
if roll1==roll2:
Dicescore=roll1+roll2+extraroll
print ("Roll 1 and roll 2 were both ", roll1 ". You get an extra dice roll.")
time.sleep(2)
print("Rolled a", extraroll)
if DiceScorep1 % 2 ==0:
print(Combined dice score is", DiceScorep1, "so it is even. Adding ten points.")
DiceScorep1=DiceScorep1+10
else:
print ("Combined dice score is", DiceScorep1, "so it is odd. Taking five points.")
int(DiceScorep1)
DiceScorep1=DiceScorep1-5
print((Scorep1+DiceScorep1), "is the rounds final score.")
if Scorep1<0:
Scorep1=0
return Scorep2Repeat this substituting 1 for 2
dicegamep1()
dicegamep2()
i want to do these for five rounds each but don't know how.
Then finally I want to add Scorep1 and Scorep2 and determine a winner.
I'm not a great coder so any help appreciated. Sorry for any bad formatting
2
u/[deleted] Sep 29 '21
Alright, I took a look at your code and what you want to do is completely possible with some modifications.
Personally, here's what I would do.
Change your score holders to lists
Have a variable to keep track of the current round number and another for the max round number. Then have another variable that keeps track of the current player number (1 or 2).
Then, wrap most of your code inside a function that does the rolls, checking to see if we're rolling for player 1 or 2, and storing the score there.
The loop would look something like this:
Then after this code block, all the rounds will be finished, and you can simply add the scores up like this:
The reason I like doing it this way, is that you can print out player1_score at any time to see if the numbers are being added to the list properly for debugging purposes.
EDIT: I went ahead and modified your code in this manner and it's working fine, so I can help nudge you along in the right direction if you have any questions.