Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

junglebooboo

Week 3, nims.py

  • one year ago
  • one year ago

  • This Question is Closed
  1. junglebooboo
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

    • one year ago
  2. Fertig
    Best Response
    You've already chosen the best response.
    Medals 1

    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

    • one year ago
  3. junglebooboo
    Best Response
    You've already chosen the best response.
    Medals 0

    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'

    • one year ago
  4. Fertig
    Best Response
    You've already chosen the best response.
    Medals 1

    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'

    • one year ago
  5. Fertig
    Best Response
    You've already chosen the best response.
    Medals 1

    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'

    • one year ago
  6. Fertig
    Best Response
    You've already chosen the best response.
    Medals 1

    One last thing... when you debug your program (look for mistakes) use print statements to find the mistakes

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.