I'm having a problem understanding which part of my code belongs to a funtions and which does not. My code is messy and I know I need to use functions to clean it up. The code below is a console game whereby a computer generates a random integer between 1 and 100 and the user user guesses that number with a limited number of guesses.eg. 5 for hard. 10 for easy.
How can I use funtions in this code?
#TODO 1: Generate a random number between 1 and 100
import random
GUESS = random.randint(1, 100)
#TODO 2: Print the guess(,for debugging)
#TODO 3: Choose the difficulty("easy" or "hard")
difficulty = input("Choose a difficulty. Type 'easy' or 'hard': ")
#TODO 4: Loop 5 times if the user typed 'hard' or 10 times if the user typed 'easy'
if difficulty == "hard":
try:
#TODO 4.1: User inputs the guesses the random number
print("You have 5 attempts remaining to guess the number.")
guess = int(input("Make a guess: "))
#TODO 4.2: Compare the guess and the random number
if guess == GUESS:
print(f"{guess} is the correct guess")
elif guess > GUESS:
print(f"{guess} is too high")
#Count dowm
for _ in range(5, 1, -1):
print(f"You have {_-1} attempts remaining to guess the number.")
guess = int(input("Make a guess: "))
if guess == GUESS:
print(f"{guess} is the correct guess")
break
elif guess > GUESS:
print(f"{guess} is too high")
elif guess < GUESS:
print(f"{guess} is too low")
elif guess < GUESS:
print(f"{guess} is too low")
#Count dowm
for _ in range(5, 1, -1):
print(f"You have {_-1} attempts remaining to guess the number.")
guess = int(input("Make a guess: "))
if guess == GUESS:
print(f"{guess} is the correct guess")
break
elif guess > GUESS:
print(f"{guess} is too high")
elif guess < GUESS:
print(f"{guess} is too low")
except ValueError:
print("Please input a number")
elif difficulty == "easy":
#TODO 4.1: User inputs the guesses the random number
try:
print("You have 10 attempts remaining to guess the number.")
guess = int(input("Make a guess: "))
#TODO 4.2: Compare the guess and the random number
if guess == GUESS:
print(f"{guess} is the correct guess")
elif guess > GUESS:
print(f"{guess} is too high")
#Count dowm
for _ in range(10, 1, -1):
print(f"You have {_-1} attempts remaining to guess the number.")
guess = int(input("Make a guess: "))
if guess == GUESS:
print(f"{guess} is the correct guess")
break
elif guess > GUESS:
print(f"{guess} is too high")
elif guess < GUESS:
print(f"{guess} is too low")
elif guess < GUESS:
print(f"{guess} is too low")
#Count dowm
for _ in range(10, 1, -1):
print(f"You have {_-1} attempts remaining to guess the number.")
guess = int(input("Make a guess: "))
if guess == GUESS:
print(f"{guess} is the correct guess")
break
elif guess > GUESS:
print(f"{guess} is too high")
elif guess < GUESS:
print(f"{guess} is too low")
#I probably have to use exceptions
#So im going to do a google search real quick
#value errors
except ValueError:
print("Please input a number")
CodePudding user response:
In pseudo code, here's an example of one function that might be useful:
function CheckGuess (INTEGER guess, INTEGER answer) returns an STRING
if guess > answer
return "too high"
if guess < answer
return "too low"
else
return "exactly"
Now, keeping in mind that a function accepts 0 or more inputs and always returns single output, and that output should always be the same, given the same input, lets see how this can be used as a functional object.
if CheckGuess(7, 10) != "exactly"
guesses = guesses - 1;
This is only one way of dozens that could be useful. Hope this helps!
CodePudding user response:
this could work, though i might have missed some functionality.:
def prompt(prompt):
prompt(input)
def guessing_game:
num_to_guess = random.randint(1,100)
while number_of_guesses > 0:
os.system("cls")
guessed_number = prompt("guess between 1 and 100")
if gessed_number == num_to_guess:
os.system("cls")
number_of_guesses = 0
print("you got it!")
else:
if number_guessed < num_to_guess:
os.system("cls")
print("to low...")
number_of_guesses =- 1
else:
os.system("cls")
print("too high...")
number_of_guesses =- 1
if number_of_guesses == 0:
print("you lost...")
quit()
if this dosent work, check for typos.