好吧我正在开发一个基本的骰子游戏,但是当它进入while循环时它不会在里面打印消息,该消息只是一个占位符。
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")
答案 0 :(得分:1)
在start函数中使用global。另外,当你试图放入ready==1
时,它将处于无限循环中!
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")
答案 1 :(得分:0)
存在范围问题。在ready
函数内未更新全局范围中定义的start()
变量。
简单演示正在发生的事情:
>>> ready = None
>>> def start():
... ready = 1
...
>>> start()
>>> print ready
None
从ready
:
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()
你也可以将其作为@ S.M全球化。 Al Mamun建议,但我不推荐它。共享数据需要全局变量,函数之间的状态。在这种情况下,不需要它 - 您的start()
函数定义ready
变量的值。它是一个单一的地方ready
被定义 - 无需使其全球化。 start()
是一个入口点,从中返回“状态”(就绪变量)是个好主意。
另见:
答案 2 :(得分:0)
当您在ready
方法中访问start()
时,您将其作为本地变量进行访问。 Python假定您使用的所有变量都是本地变量,而不是全局变量。在设置global ready
变量之前,将start()
放在ready
方法中。这将告诉python访问ready
作为全局变量。
答案 3 :(得分:0)
ready
在全局范围内定义,但您将其设置在start
函数的本地范围内。此外,您的rolldice
函数会返回1到9而不是1到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
答案 4 :(得分:0)
您正在访问一个最初被定义为全局变量的变量(就绪),然后您在启动函数中访问它而不提及您的代码('start'函数),这是一个全局变量,最后在您的同时循环您再次尝试访问一个变量,您认为该变量已赋值给它。
另一件事是你循环。当你设置ready == 1时,如果你不希望它是无限循环的话,你需要从你的循环中断一些。