from random import randint
from art import logo
print(logo)
def guesses(choice):
"""Guessing function"""
end = False
counter = choice
number = randint(1, 100)
while not end:
guess = int(input("Guess a number: "))
if counter == 1:
print(f"The number is {number}. Better luck next time.")
end = True
elif guess < number:
counter -= 1
print("Too low.")
print(f"You have {counter} guesses left.")
elif guess > number:
counter -= 1
print("Too high")
print(f"You have {counter} guesses left.")
else:
print('You are correct!')
end = True
def difficulty(choose):
"""Difficulty function"""
if choose == 'easy':
choice = 10
guesses(choice)
elif choose == 'hard':
choice = 5
guesses(choice)
else:
print("You entered an invalid choice. Go back to the start.")
choosing()
def choosing():
choose = input("How hard do you want to play? 'easy' or 'hard': ")
difficulty(choose)
print("Welcome to the number guessing game.")
print("I am thinking of a number between 1 to 100")
choosing()
Only thing that is missing from this is the play again option, which is pretty easy if you just want a simple function or even an if question to ask if you wanted to play again.
I am very surprised at how I managed to pull this off since this is the first one I did where I was given an end product and asked to make a code that could output what I was playing.
Mind you if you get to see the teachers way of making it work it is far more shorter I think and is probably more streamed lined than what I have written above.
from random import randint
from art import logo
EASY_LEVEL_TURNS = 10
HARD_LEVEL_TURNS = 5
#Function to check user's guess against actual answer.
def check_answer(guess, answer, turns):
"""checks answer against guess. Returns the number of turns remaining."""
if guess > answer:
print("Too high.")
return turns - 1
elif guess < answer:
print("Too low.")
return turns - 1
else:
print(f"You got it! The answer was {answer}.")
#Make function to set difficulty.
def set_difficulty():
level = input("Choose a difficulty. Type 'easy' or 'hard': ")
if level == "easy":
return EASY_LEVEL_TURNS
else:
return HARD_LEVEL_TURNS
def game():
print(logo)
#Choosing a random number between 1 and 100.
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
answer = randint(1, 100)
print(f"Pssst, the correct answer is {answer}")
turns = set_difficulty()
#Repeat the guessing functionality if they get it wrong.
guess = 0
while guess != answer:
print(f"You have {turns} attempts remaining to guess the number.")
#Let the user guess a number.
guess = int(input("Make a guess: "))
#Track the number of turns and reduce by 1 if they get it wrong.
turns = check_answer(guess, answer, turns)
if turns == 0:
print("You've run out of guesses, you lose.")
return
elif guess != answer:
print("Guess again.")
game()
There are differences because I always find myself personalising what I make. Hope this gets me in good stead one day to be able to really make what I need or want. Probably even get me to learn how to do AI coding since I found out python is what most of the industry use to code and train neural networks for AI creation.
But who knows only time will tell I suppose I mean I could in effect get through this course in time to be able to see another kind of coding language being created.
Time and space will just have to wait in the not too distant future.
Anyway. Thank you for your time. Hope I have not bored you too much with my little journal entry for today.
Far removed from the mind map I usually do in terms of words and sentences. This time it is code.