Hunter S
Hunter S

Reputation: 25

Python Dice Game Guidance

In my Python class, we are now doing remote projects and the most recent one has me stumped. We are to create a dice game that functions like so: 1. Enter your bet amount 2. The dice are then rolled, and the result is set as the 'point' 3. The dice are rolled again, with a 7 or 11 resulting in a win, a 2, 3, or 12 resulting in a loss, and any other outcome resulting in a reroll.

I could write this code, but we have been given a skeleton script and have to fit our solution to it. I'm struggling with this because of my limited experience with functions. I have tried to teach myself, but this skeleton script is really throwing me off. I'd appreciate any help, and I apologize if this question is not appropriate here. I'm not looking for someone to write my solution for me, just some guidance. Thanks in advance.

Here is the skeleton script:

import random

def enterBet(): # returns amount bet (1-1000)

def rollTheDice():  # returns die1, die2

def setPoint(bet):  # returns the outome of the game and the point

def playDice(bet, point):  # returns the outcome

OUTCOME_WIN = 1
OUTCOME_LOSE = 2
OUTCOME_REROLL = 3

def main():

    bet = enterBet()
    outcome, point = setPoint(bet)

    while outcome == OUTCOME_REROLL:
        outcome = playDice(bet,point)

if __name__ == "__main__":
    main()

Here is my best attempt so far:

import random

def enterBet(): # returns amount bet (1-1000)
    int(input("Please Enter Your Bet: "))

def rollTheDice():  # returns die1, die2
    die1 = random.randint(1, 6)
    die2 = random.randint(1, 6)
    return die1, die2

def setPoint(bet):  # returns the outome of the game and the point
    rollTheDice()
    point = die1 + die2
    print('You rolled a ' + point + '.')
    print('This is the new point.')
    return point, outcome

def playDice(bet, point):  # returns the outcome
    print('Point is: ' + point )
    if point == 7 or point == 11:
        outcome = OUTCOME_WIN

    elif point == 2 or point == 3 or roll == 12:
        outcome = OUTCOME_LOSE
    else:
        outcome = OUTCOME_REROLL
    return outcome

OUTCOME_WIN = 1
OUTCOME_LOSE = 2
OUTCOME_REROLL = 3

def main():

    bet = enterBet()
    outcome, point = setPoint(bet)

    while outcome == OUTCOME_REROLL:
        outcome = playDice(bet, point)

if __name__ == "__main__":
    main()

And here is the error I get:

Please Enter Your Bet: 1
Traceback (most recent call last):
  File "D:/School/CSCI 256/Project 2/p1_2.py", line 43, in <module>
    main()
  File "D:/School/CSCI 256/Project 2/p1_2.py", line 37, in main
    outcome, point = setPoint(bet)
  File "D:/School/CSCI 256/Project 2/p1_2.py", line 13, in setPoint
    point = die1 + die2
NameError: name 'die1' is not defined

Process finished with exit code 1

Upvotes: 0

Views: 1198

Answers (1)

FlamFace
FlamFace

Reputation: 455

You aren't assigning the return value of rollTheDice to variables, so the interpreter doesn't know what die1 and die2 are. The names of those variables are local to the function itself, so you would have to define them in the setPoint function too. If you change it to something like:

def setPoint(bet):  # returns the outome of the game and the point
    die1, die2 = rollTheDice()
    point = die1 + die2
    print('You rolled a ' + point + '.')
    print('This is the new point.')
    return point, outcome

it should know about those variables. However, you will still have a problem with the outcome variable which is also not defined.

You seem to be returning the point and outcome variables in a different order than is expected too, so look out for that.

Upvotes: 1

Related Questions