Media is too big
VIEW IN TELEGRAM
α΅ααα΅ α£ααα αα α¨α΅ here is our simple number guessing game python code.
α΅α codeα‘ α α°α«α α¨α΅α α₯αα½ααα...
#Game
#GuessGame
@python_pioneers
α΅α codeα‘ α α°α«α α¨α΅α α₯αα½ααα...
#Game
#GuessGame
@python_pioneers
π1
Number guessing game α Python ααα ααα΅ααα£ α₯α«αα³αα±α α¨codeα‘α section α₯α΅α² α₯ααα£
1. Importing the
- `random
Gameα‘α α¨αα«αα±α α°α αguessing range minimum α₯α maximum value α₯αα²ααα₯ α₯αα αααααα’ α¨α α¨α°αα αααα string data type int() function α αα αα αα° integerαα΅ α₯αααα¨αααα’
3. Generating a Random Number:
α αα section α°αα random number generate α₯αα²α«α¨ααα import α«α¨αααα module α°α α ααα:
Using
4. Initializing the Guess Counter:
α₯αα α°αα userα‘ αα α«α α αα α₯αα°ααα° α¨αααα£α αα α΅α variable declare α ααααα£ initial value α°αα zero α°α₯α°αααα’
#GuessGame
@python_pioneers
1. Importing the
random module:import random
- `random
moduleα import α«α¨ααα set α£α¨ααα range αα°α₯ random αα₯α α₯αα²α°α α ααα’
2. User Input for the Range:
min = int(input("Minimum number for your guess range: "))
max = int(input("Maximum number for your guess range: "))Gameα‘α α¨αα«αα±α α°α αguessing range minimum α₯α maximum value α₯αα²ααα₯ α₯αα αααααα’ α¨α α¨α°αα αααα string data type int() function α αα αα αα° integerαα΅ α₯αααα¨αααα’
3. Generating a Random Number:
rand_num = random.randint(min, max)
α αα section α°αα random number generate α₯αα²α«α¨ααα import α«α¨αααα module α°α α ααα:
Using
random.randint()4. Initializing the Guess Counter:
no_of_guesses = 0
α₯αα α°αα userα‘ αα α«α α αα α₯αα°ααα° α¨αααα£α αα α΅α variable declare α ααααα£ initial value α°αα zero α°α₯α°αααα’
#GuessGame
@python_pioneers
while True:
user_guess = int(input("Make a guess: "))
if user_guess == rand_num:
print("Congrats, you got the number!")
break
elif user_guess > max or user_guess < min:
print("Your guess is out of range!")
break
elif user_guess > rand_num:
print("You are above the number, make a lower guess.")
elif user_guess < rand_num:
print("You are below the number, make a higher guess.")
else:
print("Wrong Input!")
break
no_of_guesses += 1
- The program enters an infinite loop using
while True: where the user is repeatedly prompted to guess the number.- Checking if the Guess is Correct:
Userα‘ α«α΅αα£α αα₯α α¨ random numberα‘ αα α₯α©α α¨αα "Congrats, ..." α¨ααα message printed ααααα’
- Checking for Out-of-Range Guesses:
Userα‘ α«α΅αα£α αα₯α specified α¨ααα range ααͺ α¨αα loopα‘ ααααα’
- Providing Hints:
- If the guess is higher than the random number, the user is informed and asked to guess a lower number.
- If the guess is lower than the random number, the user is informed and asked to guess a higher number.
- Incrementing the Guess Counter:
After each guess, the
no_of_guesses counter is incremented by 1 to track the number of attempts.#GuessGame
@python_pioneers
π3β‘1π1