Python Forum
PLayer vs.PLayer game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PLayer vs.PLayer game
#1
program should display something like this:

Welcome to the game of nuts!
How many nuts are there on the table initially (10-100)? 10
There are 10 nuts on the board.
Player 1: How many nuts do you take (1-3)? 3
There are 7 nuts on the board.
Player 2: How many nuts do you take (1-3)? 3
There are 4 nuts on the board.
Player 1: How many nuts do you take (1-3)? 3
There is 1 nut on the board.
Player 2: How many nuts do you take (1-3)? 2
Player 2, you lose.

what I did was:

introduction="Welcome to the game of nuts!"
print(introduction)
x= input("How many nuts are there on the table initially(10-100)?")
print("There are "+x+" nuts on the board.")
remaining=int(x)
while remaining>0:
player=input("Which Player?")
if player=="1":
value=int(input("Player"+player+": How many nuts do you take (1-3)?"))
else:
value=int(input("Player"+player+": How many nuts do you take (1-3)?"))
while 1<=value<=3:
remaining= int(remaining)- value
remaining=str(remaining)
print("There are "+remaining+ " nuts on the board.")

but my output is a continuous loop
I m very lost please help
Reply
#2
The problem is that you have a 'while' loop (the one at the end) that executes based on the value of a variable (if value is 1, 2, or 3) but doesn't change the variable, so the loop will just repeat continuously.
However, decreasing the number on the board is something you only want to do once per turn, so the best thing to do would probably be to replace the 'while' loop at the end with an if statement.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Quoridor game. Problem: inserting player positions and walls in a checkerboard Roidesflammes 1 3,134 Aug-03-2021, 06:52 PM
Last Post: riii
  Python code isn't looping player turns Reta 7 3,614 May-19-2019, 02:27 PM
Last Post: SheeppOSU
  A little general help to make a music player MegasXLR 12 6,489 May-05-2018, 06:23 AM
Last Post: MegasXLR
  Search for Player ID and return games played and number of times disruptfwd8 3 3,404 May-03-2018, 06:45 PM
Last Post: disruptfwd8

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020