-2

Hello I am just starting to try to teach my self python and with one of the resources I read, I saw this dice game to make. So I did the basic but then I wanted to make it more full. My idea was to add a loop and have after each round it would prompt the user to enter at first q but now 0 to try to determine if it is an error in my input.

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

First, sorry if code long for this, but what I expect is 0 as an input to break the loop, what I get is having to kill my console process in spyder in order to stop this from looping

E_net4
  • 27,810
  • 13
  • 101
  • 139
Corey
  • 5
  • 4

4 Answers4

1

You have the variable name game at 2 different variable scopes and so they have different state. Try returning a game from gamestate() and comparing the value 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')  
Michael
  • 546
  • 1
  • 7
  • 19
0

You have to return game value from gamestate function and assign it in while loop. Check below code:

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()
Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19
0

In order to compare with zero as a string you need merely do something like if game == "0":. The issue you may be running into is that of extra whitespace chars like "\n". If you use input().trimspace() you'll remove extraneous chars and do comparisons with the values you want to.

Also another problem in the code is that it will enter the while loop if game does not equal "0" and so the if condition that follows will automatically not be met. So break is never hit.

perennial_noob
  • 459
  • 3
  • 14
0

You need to modify your program to convert input to int and not converting 0 to string at multiple places.changes needs to be done in while loop and gamestate function

mkrana
  • 422
  • 4
  • 10
  • yeah, you are correct, if i were to write this code again later i would totally do that but i chose to convert it into a string because i was not sure to handle errors if for example someone entered in "w" or something – Corey Apr 11 '19 at 04:56
  • best practice in python is use try/except block and in this case you should have handled ValueError: exception – mkrana Apr 11 '19 at 13:25