Python Forum
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dice game im stuck
#1
Hey, I just joined this forum and I have a controlled assessment which we're allowed to use the internet for help and my question is, how would I allow one player to 'ROLL' the dice and then allow the second player to 'ROLL' the dice. It needs to track both players scores seperately and add on what number they rolled to what they previously rolled. What it does it allows the user to roll the dice and after 6 seconds generates a random number between 1-6, after this it 

    print("Alright, please type 'ROLL' to roll the dice")

def rollDice(player):
    roll = input("")
    while True:
        if roll != "ROLL":
            print("Invalid input, please try again (case sensitive)")
            break
        else:
            print("Rolling.")
            time.sleep(2) #pauses script for 2s before continuing
            print("Rolling..")
            time.sleep(2)
            print("Rolling...")
            time.sleep(2)
            dice_num=random.randint(1,6)#picks random number between 1-6
            p1score = dice_num
            print(namep1,", you're now on space",p1score)
            if p1score or p2score == 49:
                playerWin()
            return
        
def main():
    rollDice(namep1)
    rollDice(namep2)

main()
Reply
#2
Why is line 1 indented?  In lines 24 and 25, you are calling rollDice() with the arguments "namep1" and "namep2".  Where are the arguments coming from? You could also change it to:

def main():
    rollDice(namep1, namep2)
and call rollDice
def rollDice(player1, player2):
Where is p2score defined
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
There is some code I missed out however I thought it was un-needed, and I haven't defined p2score yet. I just want it to let both players try to roll the dice and store their scores individually. I done what you suggested.
import random
import time

print("Welcome to my dice game!")
print("Player one, what is your name?") #prints message
namep1 = input("") #asks user to input name, then stores as namep1 variable
print("Okay",namep1,"nice to meet you!")
print("Alright player two, what is your name?")
namep2 = input("")
print("Nice to meet both of you, would you like to begin now? (y/n)")
begin = input("")
if begin != "y": #if begin is NOT = to y, then run exit()
    exit() #close script
else: #however if begin is equal to y, print
    print("Alright, please type 'ROLL' to roll the dice")

def rollDice(player1, player2):
    roll = input("")
    while True:
        if roll != "ROLL":
            print("Invalid input, please try again (case sensitive)")
            break
        else:
            print("Rolling.")
            time.sleep(2) #pauses script for 2s before continuing
            print("Rolling..")
            time.sleep(2)
            print("Rolling...")
            time.sleep(2)
            dice_num=random.randint(1,6)#picks random number between 1-6
            p1score = dice_num
            print(namep1,", you're now on space",p1score)
            return
        
def main():
    rollDice(namep1, namep2)

main()
Reply
#4
You don't really need dice_num. You are just using that to hold the value until you put it in p1score. You might as well just put the result of random.randint straight into p1score. That would give you:

# Handle player one.
p1score = random.randint(1, 6)
print(player1, ", you're now on space", p1score)
Note that I used player1 in the print statement instead of namep1. player1 is a parameter to the function, but namep1 is undefined. Also note my comment. It's good that you are commenting your code, but your comments are just saying the very basics of what particular lines do. I recommend comments be used for three reasons: a) docstrings (try putting a comment right after def rolldice, then type help(rollDice) after loading it into the interactive interpreter), b) an outline of what your code is doing, c) explaining anything obtuse or that was hard to figure out.

Now, take those two lines for player1/p1score, and do the exact same thing for player2/p2score.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need some help with Dice Game for daughter in school OptX 2 1,923 Feb-12-2021, 08:43 PM
Last Post: BashBedlam
  regarding dice game hola123 4 2,330 Feb-10-2021, 11:00 PM
Last Post: hola123
  Two dice Game of Pig help elliemehl 2 4,193 Feb-14-2019, 01:19 AM
Last Post: woooee
  Two Dice Pig Game in Python 3.6 Help inspired22 4 11,598 Oct-10-2018, 01:17 PM
Last Post: ichabod801
  help with while loop on dice game sean5 2 4,165 Dec-14-2017, 07:24 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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