Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 17, 2022 12:59 am GMT

Making a fancy number guessing game

This article is tailored towards new and inexperienced coders that want to start their coding careers

Demo of what we will be coding
Guessing game test

C:\Users\Josh\Desktop>guessing.pyEnter player name: JoshTheCodingAddictHi JoshTheCodingAddict Welcome to my fancy number guessing game=====================| enter 10---Easy   || enter 20---Medium || enter 30---Hard   |=====================[*] Select difficulty level: 10[*] You have 3 number of guesses left![!] Guess the lucky number: 9[-] Sorry please try again[*] You have 2 number of guesses left![!] Guess the lucky number: 8[-] Sorry please try again[*] You have 1 number of guesses left![!] Guess the lucky number: 7[-] Sorry please try again[!] Game over, type c to retry and q to quit game: c[*] You have 3 number of guesses left![!] Guess the lucky number: 7[-] Sorry please try again[*] You have 2 number of guesses left![!] Guess the lucky number: 6[-] Sorry please try again[*] You have 1 number of guesses left![!] Guess the lucky number: 5[-] Sorry please try again[!] Game over, type c to retry and q to quit game: 4[-] Wrong input[!] Game over, type c to retry and q to quit game: c[*] You have 3 number of guesses left![!] Guess the lucky number: 3[-] Sorry please try again[*] You have 2 number of guesses left![!] Guess the lucky number: 2[+] Congratulations!! you won the game[!] Thanks for playing!

Game requirements:

  • Player name
  • Game levels: Easy, Intermediate, Hard
  • Number of guesses: how many wrong guesses before ending the game
  • Fancy Instructions

Lets dive into it

Game logic

  • Users are presented a welcome message and asked to select the game difficulty
  • Then game starts and player has n number of chances to guess the lucky number
  • If the correct is number is inputed, player is presented with a congratulations message
  • If player runs out of guesses then player is presented with a message telling them they failed to guess the correct number and should retry or exit the game.

Without further ado lets get our hands dirty and write some code

Modules used:

random - used to generate pseudo random numbers

So we start by importing random

import random

Then setting our variables: player name, welcome message, congratulations message, failed message, difficulty level, lucky_number and number of guesses

player_name = input("Enter player name: ")welcome_message = f"""Hi {player_name} Welcome to my fancy number guessing game=====================| enter 10---Easy   || enter 20---Medium || enter 30---Hard   |====================="""print(welcome_message)congratulations_message = "[+] Congratulations!! you won the game
"failed_message = "[-] Sorry please try again
"difficulty = int(input("[*] Select difficulty level: "))lucky_number = random.randint(1, difficulty)guesses = 3

Now start the game loop and write the logic for guessing and comparing guesses

while guesses >= 0:    if guesses == 0:        retry = input("[!] Game over, type c to retry and q to quit game: ")        if retry == "c": guesses = 3        elif retry == "q": break        else: print("[-] Wrong input")    else:          print(f"[*] You have {guesses} number of guesses left!")        guessed = int(input("[!] Guess the lucky number: "))        if guessed == lucky_number:            print(congratulations_message)            break        else:            print(failed_message)            guesses = guesses - 1print("[!] Thanks for playing!")

Full code below

import randomplayer_name = input("Enter player name: ")welcome_message = f"""Hi {player_name} Welcome to my fancy number guessing game=====================| enter 10---Easy   || enter 20---Medium || enter 30---Hard   |====================="""print(welcome_message)congratulations_message = "[+] Congratulations!! you won the game
"failed_message = "[-] Sorry please try again
"difficulty = int(input("[*] Select difficulty level: "))lucky_number = random.randint(1, difficulty)guesses = 3while guesses >= 0: if guesses == 0: retry = input("[!] Game over, type c to retry and q to quit game: ") if retry == "c": guesses = 3 elif retry == "q": break else: print("[-] Wrong input") else: print(f"[*] You have {guesses} number of guesses left!") guessed = int(input("[!] Guess the lucky number: ")) if guessed == lucky_number: print(congratulations_message) break else: print(failed_message) guesses = guesses - 1print("[!] Thanks for playing!")

Feel free to improve on this and Happy Coding :)


Original Link: https://dev.to/joshthecodingaddict/making-a-fancy-number-guessing-game-2f4

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To