'I'm trying to get this game of Nim to work but I can't figure out why it keeps looping getUserChoice. Does anybody have any idea why this is?
When I run this code, I just get getUserChoice repeating over and over and I'm not sure why it won't move on to getComputerChoice. I can't tell if getComputerChoice just doesn't work or if there's another problem with my code. I feel like I've tried everything at this point and I've exhausted all of my resources on Google.
def displaySticks(numSticks):
print("The game currently looks like this: ")
print(" %s " % (numSticks * '|'))
def displayInstructions():
print(
"This is a game where players take turns taking sticks, starting with 11 sticks. The player who takes the last stick wins.")
def getUserChoice(numSticks):
valid = False
sticksToRemove = None
while not valid:
sticksToRemove = int(input("How many sticks do you want to remove? "))
if sticksToRemove > 3:
print("You can't remove more than three sticks at a time! "
"The current stick count is: " + str(numSticks))
elif numSticks - sticksToRemove < 0:
print("There aren't that many sticks left! ")
else:
valid = True
return sticksToRemove
def getComputerChoice(sticksToRemove):
userTurn = getUserChoice
while userTurn == True and numSticks > 0:
sticksToRemove = random.randint(1, min(3, numSticks))
numSticks -= sticksToRemove
print("The computer removed " + str(sticksToRemove) +
" sticks! The current stick count is: " + str(numSticks))
return sticksToRemove
import random
import function
numSticks = 11
sticksToRemove = 0
getUserChoice = True
def main():
while numSticks > 0:
function.displayInstructions()
print('')
function.displaySticks(numSticks)
print('')
function.getUserChoice(numSticks)
print('')
function.getComputerChoice(sticksToRemove)
if __name__ == '__main__':
main()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
