-1
from time import *
import threading
def countdown():
    global my_timer
    
    my_timer = 5
    
    for x in range(5):
        my_timer = my_timer - 1
        sleep(1)
    
    print("Time is up! Sorry!")
    
countdown_thread = threading.Thread(target = countdown)

countdown_thread.start()

while my_timer > 0:

    again="yes"
    while again=="yes":
        difficulty = input("Difficulty? easy, medium or hard? ")
        if difficulty == "easy":
                player1=int(input("Player 1 please enter a number from 1 to 99: "))
                guess= eval(input("Player 2 please enter your guess between 1 and 99 "))
                count=1
                lives=int(7)

                if guess==player1:
                    print("You rock! You guessed the number in" , count , "tries!")
                while guess !=player1:
                    count+=1
                    if lives == 1:
                        print(" No more lives, GAME OVER")
                        break
                    elif guess > player1 + 10:
                        print("Too high!")
                        lives-=1
                        print("lives remaining: ", lives)
                        guess = eval(input("Try again "))
                    elif guess < player1 - 10:
                        print("Too low!")
                        lives-=1
                        print("lives remaining: ", lives)
                        guess = eval(input("Try again "))
                    elif guess > player1:
                        print("Getting warm but still high!")
                        lives-=1
                        print("lives remaining: ", lives)
                        guess = eval(input("Try again "))
                    elif guess < player1:
                        print("Getting warm but still Low!")
                        lives-=1
                        print("lives remaining: ", lives)
                        guess = eval(input("Try again "))

                if guess==player1 or lives !=0:
                    print("You rock! You guessed the number in" , count , "tries!")



                if guess == player1 or lives==1:
                    again=str(input("Do you want to play again, type yes or no "))

                    if again == "no":
                        print("Bye")
                        break
                    elif again == "yes":
                        continue
  
                        
        elif difficulty == "medium":
            player1=int(input("Player 1 please enter a number from 1 to 199: "))
            guess= eval(input("Player 2 please enter your guess between 1 and 199 "))
            count=1
            lives=int(10)

            if guess==player1:
                print("You rock! You guessed the number in" , count , "tries!")
            while guess !=player1:
                count+=1
                if lives == 1:
                    print(" No more lives, GAME OVER")
                    break
                elif guess > player1 + 20:
                    print("Too high!")
                    lives-=1
                    print("lives remaining: ", lives)
                    guess = eval(input("Try again "))
                elif guess < player1 - 20:
                    print("Too low!")
                    lives-=1
                    print("lives remaining: ", lives)
                    guess = eval(input("Try again "))
                elif guess > player1:
                    print("Getting warm but still high!")
                    lives-=1
                    print("lives remaining: ", lives)
                    guess = eval(input("Try again "))
                elif guess < player1:
                    print("Getting warm but still Low!")
                    lives-=1
                    print("lives remaining: ", lives)
                    guess = eval(input("Try again "))

            if guess==player1 or lives !=0:
                print("You rock! You guessed the number in" , count , "tries!")



            if guess == player1 or lives==1:
                again=str(input("Do you want to play again, type yes or no "))

                if again == "no":
                    print("Bye")
                    break
                elif again == "yes":
                    continue

        
        elif difficulty == "hard":
            player1=int(input("Player 1 please enter a number from 1 to 299: "))
            guess= eval(input("Player 2 please enter your guess between 1 and 299 "))
            count=1
            lives=int(14)

            if guess==player1:
                print("You rock! You guessed the number in" , count , "tries!")
            while guess !=player1:
                count+=1
                if lives == 1:
                    print(" No more lives, GAME OVER")
                    break
                elif guess > player1 + 30:
                    print("Too high!")
                    lives-=1
                    print("lives remaining: ", lives)
                    guess = eval(input("Try again "))
                elif guess < player1 - 30:
                    print("Too low!")
                    lives-=1
                    print("lives remaining: ", lives)
                    guess = eval(input("Try again "))
                elif guess > player1:
                    print("Getting warm but still high!")
                    lives-=1
                    print("lives remaining: ", lives)
                    guess = eval(input("Try again "))
                elif guess < player1:
                    print("Getting warm but still Low!")
                    lives-=1
                    print("lives remaining: ", lives)
                    guess = eval(input("Try again "))

            if guess==player1 or lives !=0:
                print("You rock! You guessed the number in" , count , "tries!")


            if guess == player1 or lives==1:
                again=str(input("Do you want to play again, type yes or no "))

                if again == "no":
                    print("Bye")
                    break
                elif again == "yes":
                    continue 

I'm unable to make break the loop when my timer comes to an end.

I tried to include if my_timer==0 break but it doesn't really work for some reason, and the program just continues on.

I'm really confused by this; can you help me to understand?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Doli
  • 1
  • 1
    https://stackoverflow.com/questions/653509/breaking-out-of-nested-loops may help. – hiro protagonist Jul 21 '20 at 09:35
  • Please take a look at the [mcve] and [ask] help pages. It's not readily apparent what exactly you are asking – especially *which* loop you want to break out of and what happens instead. – MisterMiyagi Jul 21 '20 at 13:20

1 Answers1

0

Unfortunately I can't comment as I don't have a reputation greater than 50 due to being relatively new here.

I don't have a solution as to why the timer is not stopping the program when it runs out but I think I know the reason.

I think it is due to the fact that upon first entering the while my_timer>0:, my_timer is greater than 0 which means it goes onto the next while loop which then runs your main body. This means the my_timer variable isn't continuously checked until the second while loop is re-checked. So basically you can't break from the first loop until you break out of the second loop after the timer has finished.

When I ran your code I also found a couple issues:

1)Even if I answer "no" to playing again, if the timer is still running then I am put into the game to select a difficulty again

2)If the timer ends and I answer "yes" to playing again I can continue playing as long as I like

3)After selecting a difficulty, I can still specify a number outside the range you want.

Although the following code does not solve the problem of the timer not stopping the game. It will stop after the timer runs out even if I choose "yes" to playing again and it will break and stop the timer if i choose no to playing again. I also think the code is a little neater as it has less repitition.

import threading
import time


def countdown():
    global my_timer
    global stop_thread
    while True:
        time.sleep(1)
        my_timer -= 1
        if my_timer == 0:
            print("Time is up! Sorry!")
            break
        elif stop_thread:
            break


def process_guess(lives, guess, player1, boundary):
    player2_lives = lives
    guess_count = 1

    while guess != player1:
        guess_count += 1
        if guess > player1 + boundary:
            print("Too high!")
        elif guess < player1 - boundary:
            print("Too low!")
        elif guess > player1:
            print("Getting warm but still high!")
        elif guess < player1:
            print("Getting warm but still Low!")

        player2_lives -= 1
        if player2_lives <= 0:
            print(" No more lives, GAME OVER")
            return None
        else:
            print("lives remaining: ", player2_lives)
            guess = eval(input("Try again "))

    else:
        if lives != 0:
            print("You rock! You guessed the number in", guess_count, "tries!")

        if lives >= 1:
            again = str(input("Do you want to play again?, type yes or no "))

            if again == "no":
                print("Bye")
                return None
            elif again == "yes":
                return True


def guessing_game():
    countdown_thread = threading.Thread(target=countdown)
    countdown_thread.start()
    while my_timer > 0:
        global stop_thread
        guess_result = None
        while True:
            difficulty = input("Difficulty? easy, medium or hard? ")
            while difficulty not in ["easy", "medium", "hard"]:
                difficulty = input("Difficulty? easy, medium or hard? ")

            if difficulty == "easy":
                boundary = 10
                player1 = int(input("Player 1 please enter a number from 1 to 99: "))
                guess = eval(input("Player 2 please enter your guess between 1 and 99 "))
                lives = 7
                guess_result = process_guess(lives=lives, guess=guess, player1=player1, boundary=boundary)

            elif difficulty == "medium":
                boundary = 20
                player1 = int(input("Player 1 please enter a number from 1 to 199: "))
                guess = eval(input("Player 2 please enter your guess between 1 and 199 "))
                lives = 10
                guess_result = process_guess(lives=lives, guess=guess, player1=player1, boundary=boundary)

            elif difficulty == "hard":
                boundary = 30
                player1 = int(input("Player 1 please enter a number from 1 to 299: "))
                guess = eval(input("Player 2 please enter your guess between 1 and 299 "))
                lives = 14
                guess_result = process_guess(lives=lives, guess=guess, player1=player1, boundary=boundary)

            break

        if guess_result:
            continue
        else:
            stop_thread = True
            break


if __name__ == "__main__":
    my_timer = 5
    stop_thread = False
    guessing_game()

If you wanted to you could even reduce the first two while loops to one by using:

def guessing_game():
    countdown_thread = threading.Thread(target=countdown)
    countdown_thread.start()
    global stop_thread
    while True and my_timer > 0:
        guess_result = None
        difficulty = input("Difficulty? easy, medium or hard? ")
        while difficulty not in ["easy", "medium", "hard"]:
            difficulty = input("Difficulty? easy, medium or hard? ")

        if difficulty == "easy":
            boundary = 10
            player1 = int(input("Player 1 please enter a number from 1 to 99: "))
            guess = eval(input("Player 2 please enter your guess between 1 and 99 "))
            lives = 7
            guess_result = process_guess(lives=lives, guess=guess, player1=player1, boundary=boundary)

        elif difficulty == "medium":
            boundary = 20
            player1 = int(input("Player 1 please enter a number from 1 to 199: "))
            guess = eval(input("Player 2 please enter your guess between 1 and 199 "))
            lives = 10
            guess_result = process_guess(lives=lives, guess=guess, player1=player1, boundary=boundary)

        elif difficulty == "hard":
            boundary = 30
            player1 = int(input("Player 1 please enter a number from 1 to 299: "))
            guess = eval(input("Player 2 please enter your guess between 1 and 299 "))
            lives = 14
            guess_result = process_guess(lives=lives, guess=guess, player1=player1, boundary=boundary)

        if guess_result:
            continue
        else:
            break
    stop_thread = True

To fix the problem of people being able to enter numbers outside the ones you specify you can use a while loop similar to how I have for specifying the difficulty. Alternatively you can use assertions but that's more for raising errors.

Hope that helps, sorry I couldn't make the timer stop the program.

C.Holmes
  • 13
  • 3