The problem:
We have to create a game, where a user inputs the number of pencils they want to use. Expand your program by creating a loop. Each player takes turns removing pencils until 0 pencils remain on the table. Each iteration prints 2 lines: lines with pencils (vertical bars) and whose turn is now by printing the NameX's turn substring.
How many pencils would you like to use:
> 5
Who will be the first (John, Jack):
> John
|||||
John's turn:
> 2
|||
Jack's turn:
> 1
||
John's turn:
> 2
My solution:
person = input("Who will be the first (John, Jack):")
print("|" * number_of_pencil)
print(f"{person}'s turn")
while number_of_pencil > 1:
pencil = int(input())
number_of_pencil -= pencil
print("|" * number_of_pencil)
if person == "John":
print("Jack's turn")
person = "Jack"
else:
print("John's turn")
person = "John"
The output:
Who will be the first (John, Jack):> John
|||||
John's turn
> 2
|||
Jack's turn
> 1
||
John's turn
> 2
Jack's turn
Suggestion required: How should I break the loop?