def number_dif(count, num, num_choose):
if num_choose > num:
print("Too high.")
print("Guess again.")
print(f"You have {count} attempts remaining to guess the number")
return False
elif num_choose < num:
print("Too low.")
print("Guess again.")
print(f"You have {count} attempts remaining to guess the number")
return False
elif num_choose == num:
return True
import random
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
difficulty = input("Choose a difficulty. Type 'easy' or 'hard': ")
random_number = random.randint(1, 100)
if difficulty == 'easy':
print("You have 10 attempts remaining to guess the number.")
for i in range(9, 0, -1):
guess = int(input("Make a guess: "))
result = number_dif(i,random_number, guess)
if result == True:
print(f"You got it! The answer was {random_number}")
break
if result != True:
print(f"You have failed to guess the number. The answer was {random_number}")
elif difficulty == 'hard':
print("You have 5 attempts remaining to guess the number.")
for i in range(4, 0, -1):
guess = int(input("Make a guess: "))
result = number_dif(i,random_number, guess)
if result == True:
print(f"You got it! The answer was {random_number}.")
break
if result != True:
print(f"You've run out of guesses. The answer was {random_number}")
else:
print("You did not choose the right difficulty, run the game again")
number_dif 함수에 유저가 입력한 숫자와 맞춰야 하는 숫자를 비교한다
못 맞추면, False를 return하고 맞추면 True를 return 한다
이 함수를 for문에 넣어서, easy를 선택했을 땐 10번을 반복하고, hard를 선택했을 땐 5번을 반복한다