Python Tutorial Beginners: Build a Fun Guessing Game! – In this post, we’ll walk you through building a simple guessing game in Python, perfect for absolute beginners looking to learn basic concepts like loops, conditions, and random values. Follow along and get a fun introduction to Python programming
import random
def main():
userContinue="Y"
while userContinue=="Y":
actualNumber=random.randint(1,50)
step=0
flag=False
while step<5:
print("Chance number ",(step+1)," of 5 ")
userInput=int(input("Enter your choice. " ))
if userInput==actualNumber:
print("Yeah, you guessed it right, you are the winner.")
flag=True
break
else:
print("Oops you guessed it incorrect")
if userInput>actualNumber:
print("You have guessed a larger number")
elif userInput<actualNumber:
print("You have guessed a smaller number")
step=step+1
if flag==False:
print("You have lost the game. The actual number was ", actualNumber)
userContinue=input("Do you want to continue? (Y/N) ")
main()