Skip to content

Python Journey

My Coding Blog

Menu
  • Links
Menu

Python Project1: Guess Number

Posted on December 31, 2024 by coden00b
Code:
import random
import time
correctanswer = random.randint(1,20)
guesscount = 1
print(“I will be selecting numbers between 1 and 20”)
print(“Picking a number…”)
time.sleep(2)
guess = int(input(“Guess a number: “))
while guess != correctanswer:
    guesscount += 1
    if guess < correctanswer:
        guess = int(input(“You need to make it higher. Try again: “))
    else:
        guess = int(input(“You need to make it lower. Try again: “))
print(f”Congrats. The correct answer is {correctanswer}. It took you total of {guesscount} guesses.”)
Sample Output:
I will be selecting numbers between 1 and 20
Picking a number…
Guess a number: 17
You need to make it lower. Try again: 12
You need to make it higher. Try again: 13
You need to make it higher. Try again: 15
You need to make it higher. Try again: 16
Congrats. The correct answer is 16. It took you total of 5 guesses.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Python Project1: Guess Number
  • The Zen of Python
  • Python>>String Interpolation and Formatting
  • Python>>String Methods
  • Python>>String Concatenation

Archives

  • December 2024
  • January 2023
  • August 2022

Tags

.format attributes concatenation count data type find float fstring guess indexing interpolation len lower methods number project python random replace slicing string strings template time upper zen
© 2025 Python Journey | Powered by Minimalist Blog WordPress Theme