파이썬 - 14일차 - 초급 - 숫자 업다운 게임 프로젝트

Shy·2023년 2월 20일

Python

목록 보기
2/9

Higher-Lower 게임 소개 및 프로그래밍 요구 사항

두 가지 검색어 중에 어떤 검색어가 더 많이 검색됬는지 입력하는 것이다.
검색어가 아니어도 팔로어 같은 걸로 제작 가능하다. (여기선 팔로어 수로 한다.)

위의 두 데이터를 활용하여 제작한다.

  • 문제를 작게 나누어 고민해 하나씩 해결해 간다.
  • 프로그래밍 할 목록을 제작하여 쉬운 것 부터 해결해간다.
  • 주석을 달아서, 어떻게 해결 할 지 생각한다.
  • 그 다음에 코드를 작성하고, 실행하고, 수정한다.
  • 첫 번째, art_logo를 띄워야 한다.
  • 데이터를 랜덤으로 가져와 출력해야 한다.
  • 유저에게 문제를 낸다.
  • 문제에 대한 답이 맞는지 체크 후에, 답을 피드백 한다.
  • 유저가 답을 맞췄으면 앞의 문제의 'B'가 뒷 문제의 비교대상 'A'가 되고, 게임을 계속 진행한다.
  • 답이 틀렸으면, 게임이 종료되고, 최종 스코어를 알려준다.

코드

from game_data import data
import random
from art import logo, vs
from replit import clear

def get_random_account():
  """Get data from random account"""
  return random.choice(data)

def format_data(account):
  """Format account into printable format: name, description and country"""
  name = account["name"]
  description = account["description"]
  country = account["country"]
  # print(f'{name}: {account["follower_count"]}')
  return f"{name}, a {description}, from {country}"

def check_answer(guess, a_followers, b_followers):
  """Checks followers against user's guess 
  and returns True if they got it right.
  Or False if they got it wrong.""" 
  if a_followers > b_followers:
    return guess == "a"
  else:
    return guess == "b"


def game():
  print(logo)
  score = 0
  game_should_continue = True
  account_a = get_random_account()
  account_b = get_random_account()

  while game_should_continue:
    account_a = account_b
    account_b = get_random_account()

    while account_a == account_b:
      account_b = get_random_account()

    print(f"Compare A: {format_data(account_a)}.")
    print(vs)
    print(f"Against B: {format_data(account_b)}.")
    
    guess = input("Who has more followers? Type 'A' or 'B': ").lower()
    a_follower_count = account_a["follower_count"]
    b_follower_count = account_b["follower_count"]
    is_correct = check_answer(guess, a_follower_count, b_follower_count)

    clear()
    print(logo)
    if is_correct:
      score += 1
      print(f"You're right! Current score: {score}.")
    else:
      game_should_continue = False
      print(f"Sorry, that's wrong. Final score: {score}")

game()
profile
신입사원...

0개의 댓글