furas

Python - guess number without recursion (Stackoverflow)

Jan 19th, 2026 (edited)
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. # date: 2026.01.20
  2. # [python - I m new to coding and I ran into a problem I cant understand:I got this Programm and my funktion play doesnt work it just starts again - Stack Overflow](https://stackoverflow.com/questions/79867245/i-m-new-to-coding-and-i-ran-into-a-problem-i-cant-understandi-got-this-programm)
  3.  
  4. import random
  5.  
  6.  
  7. def question():
  8.     num = random.randint(0, 100)
  9.     score = 0
  10.  
  11.     print("Guess the Number")
  12.  
  13.     while True:
  14.         try:
  15.             guess = int(input("> "))
  16.             score = score + 1
  17.             if num < guess:
  18.                 print("The Number is Smaller")
  19.             if num > guess:
  20.                 print("The Number is Bigger")
  21.             if num == guess:
  22.                 print(f"Congratulations you found the Number within {score} Attempts")
  23.                 return
  24.         except ValueError as ex:
  25.             print("Please enter a Number")
  26.  
  27.  
  28. def play_again():
  29.     while True:
  30.         again = input("Would you like to play again (Yes/No)? ")
  31.         if again == "Yes":
  32.             return True
  33.         if again == "No":
  34.             return False
  35.         else:
  36.             print("Please use Yes or No")
  37.  
  38.  
  39. def main():
  40.     try:
  41.         while True:
  42.             question()
  43.             if not play_again():
  44.                 break
  45.     except KeyboardInterrupt:  # it can catch Ctrl+C to stop program
  46.         print("\nInterrupted by user")
  47.  
  48.  
  49. main()
Advertisement
Add Comment
Please, Sign In to add comment