anonymous
  • anonymous
Week 3, nims.py
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
numStones = 100 def isValid(player1): try: player1 = int(player1) if player1 in range(6): return True else: return False except ValueError: return False while numStones > 0: print "Welcome to this game of stones, there are now %s stones remaining in the pile" % (numStones) player1 = isValid(raw_input('enter integer')) while not isValid(player1): print 'boo' Here's my code. the def isValid returns True and False as i expect it to, but I cant seem so use it properly inside my loop. It does not get to the while is not valid statement. How come?
anonymous
  • anonymous
Not sure why you would use a while statement for printing "boo". Try an if statement. Also, since you call the isValid on the users input, player one is a boolean => either true or false, so when you call the isValid() with player1, you are going through the except ValueError and getting False returned
anonymous
  • anonymous
the goal isnt to print boo.. that is just to see if ii works.. which it doesnt.. Im trying to make a game of stones.. here is the entire code: numStones = 100 def isValid(player1): try: player1 = int(player1) if player1 in range(6): return True else: return False except ValueError: return False while numStones > 0: print "Welcome to this game of stones, there are now %s stones remaining in the pile" % (numStones) player1 = isValid(raw_input('enter integer')) while not isValid(player1): print ' that is not a valid input' player1 = isValid(raw_input('enter integer')) if isValid(player1): numStones = numStones - int(player1) if numStones == 0: #player 1 wins print 'Congratulations player 1, you win' #player 2's turn print "Player 2's turn" player2 = 0 while not isValid: #ask print print print player2 = raw_input('Enter a value between 1 and 5') if not isValid(player2): print 'that is not a valid input' numStones = numStones - int(player2) if numStones == 0: #player 2 wins print ' Congratulations, player 2 wins the game' print 'Game over, thank you for playing'

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
Ok, I'll look at the rest too, but for now to your first question: while numStones > 0: print "Welcome to this game of stones, there are now %s stones remaining in the pile" % (numStones) player1 = isValid(raw_input('enter integer')) <<== player one is assinged as a boolean!!! True or False Value while not isValid(player1): <<== calling isValid() with a boolean print ' that is not a valid input' results in True being returned, since if you convert a boolean into an integer it is 1 or 0 Try not calling isValid on raw_input and using and using an if instead of a while statement like this: while numStones > 0: print "Welcome to this game of stones, there are now %s stones remaining in the pile" % (numStones) player1 = raw_input('enter integer') if not isValid(player1): print ' that is not a valid input'
anonymous
  • anonymous
Or on second thought, now that I've read through more of the code: Try this... It is a cleaned up version of what you sent, but it doesn't work as the game is intended. Run it a few times, and you'll see what I mean. Think about how you could fix the problems, if you need any help with that just ask And watch out for indentation mistakes. In python it is important to get that right. numStones = 100 def isValid(player): try: player = int(player) if player in range(6): return True else: return False except ValueError: return False while numStones > 0: print "Welcome to this game of stones, there are now %s stones remaining in the pile" % (numStones) player1 = raw_input('Enter a value between 1 and 5') while not isValid(player1): print ' that is not a valid input' player1 = raw_input('Enter a value between 1 and 5') if isValid(player1): numStones -= int(player1) if numStones == 0: #player 1 wins print 'Congratulations player 1, you win' print 'Game over, thank you for playing' break #player 2's turn print "Player 2's turn" player2 = raw_input('Enter a value between 1 and 5') while not isValid(player2): print 'that is not a valid input' player2 = raw_input('Enter a value between 1 and 5') if isValid(player2): numStones -= int(player2) if numStones == 0: #player 2 wins print ' Congratulations, player 2 wins the game' print 'Game over, thank you for playing'
anonymous
  • anonymous
One last thing... when you debug your program (look for mistakes) use print statements to find the mistakes

Looking for something else?

Not the answer you are looking for? Search for more explanations.