Python Forum
dice game im stuck - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: dice game im stuck (/thread-3302.html)



dice game im stuck - sylerr - May-12-2017

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()



RE: dice game im stuck - sparkz_alot - May-12-2017

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


RE: dice game im stuck - sylerr - May-12-2017

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()



RE: dice game im stuck - ichabod801 - May-12-2017

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.