-1

I have made a code for a game of pickup sticks that can be played in two modes:

  1. Single Player (against the computer)
  2. Multiplayer (local game)

I seem to be having a problem with the program in the single-player mode. The computer outputs negative numbers where it is logically not possible and I would like to prevent this from happening. Here is the code:

            import random
            sticks = 27

            choice = input("Do you wanna play multiplayer or against the computer? Type M for multiplayer and S for against the computer: ")

            if choice == "M":
                while sticks > 0: 
                    i = int(input("Between 1 and 3, how many sticks do you want to pick : ")) 
                    while i < 1 or i > 3: 
                        print("It has to be between 1 and 3! You NOOB!") 
                        i = int(input("Between 1 and 3, how many sticks do you want to pick : "))
                    sticks = sticks - i 
                    print(sticks," stick(s) left...") 
                print("You lost! hahaha...")

            elif choice == "S":

                    while sticks > 0: 
                        i = int(input("Between 1 and 3, how many sticks do you want to pick : ")) 
                        while i < 1 or i > 3:  
                            print("It has to be between 1 and 3! You NOOB!") 
                            i = int(input("Between 1 and 3, how many sticks do you want to pick : "))
                        sticks = sticks - i 
                        print(sticks," stick(s) left...") 
                        if sticks == 0:
                            print("You lost! hahaha...")
                            break
                        else:    
                            num = random.randint(1,3)
                            print("Computer's Turn...")
                            print("The computer picked ",num, " stick(s)...")
                            sticks = sticks - num
                            if sticks < 1:
                                print("You won... Damn you are good!")
                                print(sticks," stick(s) left...") 

And this is the output:

Do you wanna play multiplayer or against the computer? Type M for multiplayer and S for against the computer: S
Between 1 and 3, how many sticks do you want to pick : 3
24  stick(s) left...
Computer's Turn...
The computer picked  2  stick(s)...
22  stick(s) left...
Between 1 and 3, how many sticks do you want to pick : 3
19  stick(s) left...
Computer's Turn...
The computer picked  1  stick(s)...
18  stick(s) left...
Between 1 and 3, how many sticks do you want to pick : 3
15  stick(s) left...
Computer's Turn...
The computer picked  1  stick(s)...
14  stick(s) left...
Between 1 and 3, how many sticks do you want to pick : 3
11  stick(s) left...
Computer's Turn...
The computer picked  2  stick(s)...
9  stick(s) left...
Between 1 and 3, how many sticks do you want to pick : 3
6  stick(s) left...
Computer's Turn...
The computer picked  2  stick(s)...
4  stick(s) left...
Between 1 and 3, how many sticks do you want to pick : 2
2  stick(s) left...
Computer's Turn...
The computer picked  3  stick(s)...
You won... Damn you are good!
-1  stick(s) left...

As you can see, the game displays a negative number where it should either just end and say you won or the computer should not be able to go into negative numbers. Can someone tell me what I'm doing wrong?

1 Answers1

0

You'd have to implement an "if" statement somewhere when "sticks < 3". Think about it, your first while loop will still run if sticks = 2, since sticks > 0. So what happens if the the next i chosen is 3? 2-3 = -1. Therefore, once sticks = 2, you need to implement some sort of statement saying i can only equal to 2 or 1, and do the same for sticks = 1 as well

albert
  • 1,158
  • 3
  • 15
  • 35