1

Ok so im working on a basic dice game, But when it gets to the while loop it doesnt print the message inside, The message is just a placeholder.

    dicenumber = None
    numberchoice = None
    ready = None
    test = "Lol"

    class playerClass():
        points = 0


    class enemyClass():
        point = 0


    def rolldice():
        dicenumber = dicenumber.randint(1,9)

    def start():
        print("Hello welcome to the dice game.")
        print("The goal of the game is to guess what number the dice will land on.")
        print("The option are 1 to 6 and the game is won by getting 3 points.")
        print()
        print("Are you ready to play?")
        print("1 - Yes")
        print("2 - No")
        ready = int(input())



    start()

    while ready == 1:
        print("hello")
  • why don't you just set, some_var = raw_input("Are you read to play, 1 - yes, 2 - no") then return some_var from your function. Typically don't use a global var. – TTT Apr 07 '14 at 02:02

5 Answers5

1

Use global inside your start function. Also, as you were trying to put while ready==1, it will be in infinite loop!

dicenumber = None
numberchoice = None
ready = None
test = "Lol"

class playerClass():
        points = 0


class enemyClass():
        point = 0


def rolldice():
        dicenumber = dicenumber.randint(1,9)

def start():
        global ready
        print("Hello welcome to the dice game.")
        print("The goal of the game is to guess what number the dice will land on.")
        print("The option are 1 to 6 and the game is won by getting 3 points.")
        print()
        print("Are you ready to play?")
        print("1 - Yes")
        print("2 - No")
        ready = int(input())



start()

while ready == 1:
        print("hello")
Sharif Mamun
  • 3,508
  • 5
  • 32
  • 51
0

There is a scoping issue. ready variable defined in a global scope is not updated inside the start() function.

Simple demo of what is happening:

>>> ready = None
>>> def start():
...     ready = 1
... 
>>> start()
>>> print ready
None

Better return ready variable from the start():

def start():
    print("Hello welcome to the dice game.")
    print("The goal of the game is to guess what number the dice will land on.")
    print("The option are 1 to 6 and the game is won by getting 3 points.")
    print()
    print("Are you ready to play?")
    print("1 - Yes")
    print("2 - No")
    return int(input())

ready = start()

You can also make it global as @S.M. Al Mamun suggested, but i would not recommend it. Global variables are needed for sharing data, state between functions. In this case there is no need for it - your start() function defines a value for the ready variable. It is a single one place where ready is defined - no need to make it global. start() is an entry point and it would be a good idea to return a "state" (ready variable) from it.

See also:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

When you access ready inside the start() method, you are accessing it as a local variable. Python assumes that all variables you use are local, not global. Put global ready in the start() method before you set the ready variable. This will tell python to access ready as a global variable.

Yep_It's_Me
  • 4,494
  • 4
  • 43
  • 66
0

ready is defined in the global scope, but you are setting it in the local scope of the start function. Also, your rolldice function returns a number from 1 to 9 instead of 1 to 6.

from random import randint

class Player:
    def __init__(self, name):
        self.name = name
        self.points = 0
        self.won = False

    def add_point(self):
        self.points += 1
        self.won = True

print('''
Hello welcome to the dice game.
The goal of the game is to guess what number the dice will land on.
The options are 1 to 6 and the game is won by getting 3 points.

Are you ready to play?
0 - No
1 - Yes'''
ready = int(input())

if ready:
    players = []
    number_of_players = int(input('How many players? '))
    for i in range(number_of_players):
        name = input('What is the name of player {}?'.format(i+1))
        players.append(Player(name))
    winners = []

while ready:
    random_number = randint(1, 6)
    for player in players:
        guess = int(input('What is your guess, {}?'.format(player.name)))
        if guess == random_number:
            player.add_point()
        if player.won:
            winners.append(player.name)
    if winners:
        print('Winners: {}'.format(', '.join(winners)))
        break
Scorpion_God
  • 1,499
  • 10
  • 15
0

You are accessing a variable(ready) which was defined originally as a global variable, then you are accessing it in your start function without mentioning in your code('start' function) that is a global variable and finally in your while loop you are again trying to access a variable which you assume it has a value assigned to it.

The other thing is you while loop. when you set ready==1, you need to some where break from your loop if you don't want it to be infinite loop.

hamid
  • 469
  • 4
  • 6