100일 간의 코드: 2022년을 위한 완벽한 Python Pro 부트캠프 - 12일차(숫자 추측 게임)
프로젝트 12 - 숫자 추측 게임
from random import randint
from replit import clear
from art import logo
# Global scope
EASY_LEVEL_TURNS = 10
HARD_LEVEL_TURNS = 5
# Function to check users guess against answer
def check_answer(guess, answer, turn,):
if guess > answer:
print("Too high")
return turn -1
elif guess < answer:
print("Too low")
return turn -1
else:
print(f"You got it, the answer was {answer} ")
# Function to set difficulty
def set_difficulty():
level = input("Choose a difficulty level. Type 'easy' or 'hard': ")
if level == 'easy':
return EASY_LEVEL_TURNS
else:
return HARD_LEVEL_TURNS
# Define game function
def play_game():
print (logo)
--Choosing a random number between 1 and 100
print("Welcome to the Number Guessing Game")
print("Choose a random number between 1 and 100: ")
answer = randint (1, 100)
# Print statement used for initial testing
print(f"Psssst, the correct number is {answer}" )
# Repeat the guesing functionality if the answer is wrong
turns = set_difficulty()
guess = 0
while guess != answer:
print(f"You have {turns} attempts remaing to guess the answer")
# let the player guess a number
guess = int(input("Guess again:"))
# 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 turns, You Lose")
return
elif guess != answer:
print(f"Guess again:")
clear()
play_game()
Reference
이 문제에 관하여(100일 간의 코드: 2022년을 위한 완벽한 Python Pro 부트캠프 - 12일차(숫자 추측 게임)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/mike_kameta_aed62d48c2d0f/100-days-of-code-the-complete-python-pro-bootcamp-for-2022-day-12-4g2c
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
from random import randint
from replit import clear
from art import logo
# Global scope
EASY_LEVEL_TURNS = 10
HARD_LEVEL_TURNS = 5
# Function to check users guess against answer
def check_answer(guess, answer, turn,):
if guess > answer:
print("Too high")
return turn -1
elif guess < answer:
print("Too low")
return turn -1
else:
print(f"You got it, the answer was {answer} ")
# Function to set difficulty
def set_difficulty():
level = input("Choose a difficulty level. Type 'easy' or 'hard': ")
if level == 'easy':
return EASY_LEVEL_TURNS
else:
return HARD_LEVEL_TURNS
# Define game function
def play_game():
print (logo)
--Choosing a random number between 1 and 100
print("Welcome to the Number Guessing Game")
print("Choose a random number between 1 and 100: ")
answer = randint (1, 100)
# Print statement used for initial testing
print(f"Psssst, the correct number is {answer}" )
# Repeat the guesing functionality if the answer is wrong
turns = set_difficulty()
guess = 0
while guess != answer:
print(f"You have {turns} attempts remaing to guess the answer")
# let the player guess a number
guess = int(input("Guess again:"))
# 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 turns, You Lose")
return
elif guess != answer:
print(f"Guess again:")
clear()
play_game()
Reference
이 문제에 관하여(100일 간의 코드: 2022년을 위한 완벽한 Python Pro 부트캠프 - 12일차(숫자 추측 게임)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/mike_kameta_aed62d48c2d0f/100-days-of-code-the-complete-python-pro-bootcamp-for-2022-day-12-4g2c텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)