Guess my number in games with disabilities

As part of my course at the university, I study the python problem that I tried (to repeat) to write down this assumption that the game number ends if the user cannot correctly guess within 5 attempts:

# Guess My Number Mod 5 tries or bust import random print("\nI'm thinking of a number between 1 and 100.") print("Try to guess it in five tries or less") my_number = random.randint(1, 100) guess = int(input("Go on, Take a guess, I dare ya ")) tries = 1 while guess != my_number: if guess > my_number: print("Lower...") else: print("Higher...") guess = int(input("Go on, Take a guess, I dare ya ")) tries += 1 if tries==5: input("You failed to guess the number was it that hard?\n Press any key to exit!)" print("Well done you guessed correctly!The number was", my_number) print("And it only took you", tries, "tries!\n") input("\n\nPress the enter key to exit.") 

I assume that the reason for the termination does not work, since the if statement is outside the while loop, and I cannot force it to take effect.

Also the syntax is invalid because I am tired and cannot find it.

If possible, you guys can give me tips on how to fix what I want to do, as I am more likely to find out this way.

+4
source share
1 answer

You are looking for a break from the loop when a certain condition is reached.

 if condition: # do something break # brings you out of the loop 
+2
source

Source: https://habr.com/ru/post/1444876/


All Articles