您好,我刚刚开始尝试教自己的python,并通过阅读的资源之一,看到了这个骰子游戏。所以我做了基本的练习,但是后来我想使其更加饱满。我的想法是添加一个循环,并在每个回合后提示用户在第一个q处输入,但现在输入0以尝试确定输入中是否有错误。
def gamestate():
print('enter 0 if you would like to quit anything else to continue')
game = input()
print(game == 0) # diagnostic to check if value is correct
print(type(game)) #diagnostic to make sure type is correct
print(game != str(0))
def play():
print('do you want to play a game enter yes to start')
game = '1' #filler value
game=input()
str(game)
if game == "yes": #confirms start of the game
Dice()
else:
print('Ok Goodbye') #plays game anyways will fix after loop issue
gamestate()
______________________________________________________________
while game !=str(0): #cannot escape loop for some reason
if game == str(0) :
break #to break
Dice()
gamestate()
print('ok good bye')
___________________________________________________________
play()
首先,很抱歉,如果这段代码很长,但是我希望输入0作为中断循环的输入,那么我所得到的是必须杀死spyder中的控制台进程才能阻止其循环
答案 0 :(得分:1)
您在2个不同的变量范围内有变量名游戏,因此它们具有不同的状态。尝试从gamestate()返回游戏并比较该值 Short description of the scoping rules?
def gamestate():
print('enter 0 if you would like to quit anything else to continue')
game = input()
print(game == 0) # diagnostic to check if value is correct
print(type(game)) #diagnostic to make sure type is correct
print(game != str(0))
return game
while game !=str(0): #cannot escape loop for some reason
if gamestate() == str(0) :
break #to break
Dice()
print('ok good bye')
答案 1 :(得分:0)
您可以从gamestate函数返回game
的值。
def gamestate():
print('enter 0 if you would like to quit anything else to continue')
game = input()
print(game == 0) # diagnostic to check if value is correct
print(type(game)) #diagnostic to make sure type is correct
print(game != str(0))
return game
while game !=str(0):
Dice()
game=gamestate()
您可以将game
变量设置为全局变量
def gamestate():
print('enter 0 if you would like to quit anything else to continue')
global game
game = input()
print(game == 0) # diagnostic to check if value is correct
print(type(game)) #diagnostic to make sure type is correct
print(game != str(0))
return game
def play():
print('do you want to play a game enter yes to start')
global game
game = '1' #filler value
game=input()
str(game)
if game == "yes": #confirms start of the game
Dice()
else:
print('Ok Goodbye') #plays game anyways will fix after loop issue
while game !=str(0): #cannot escape loop for some reason
Dice()
gamestate()
print('ok good bye')
play()
答案 2 :(得分:0)
您必须从game
函数返回gamestate
值,并在while循环中分配它。检查以下代码:
def gamestate():
print('enter 0 if you would like to quit anything else to continue')
game = input()
print(game == 0) # diagnostic to check if value is correct
print(type(game)) #diagnostic to make sure type is correct
print(game != str(0))
return game
def play():
print('do you want to play a game enter yes to start')
game = '1' #filler value
game=input()
str(game)
if game == "yes": #confirms start of the game
Dice()
else:
print('Ok Goodbye') #plays game anyways will fix after loop issue
gamestate()
while game !=str(0): #cannot escape loop for some reason
if game == str(0) :
break #to break
Dice()
game = gamestate()
print('ok good bye')
play()
答案 3 :(得分:0)
为了将零作为字符串进行比较,您只需要执行if game == "0":
之类的操作即可。您可能遇到的问题是多余的空格字符,例如“ \ n”。如果您使用input().trimspace()
,则将删除多余的字符,并与所需的值进行比较。
代码中的另一个问题是,如果游戏不等于“ 0”,它将进入while循环,因此将自动不满足后面的if条件。因此break
从未被击中。
答案 4 :(得分:0)
您需要修改程序以将输入转换为int而不是在多个位置将0转换为字符串。需要在while循环和gamestate函数中进行更改