此次練習的 Project 為 Higher or Lower Game

http://www.higherlowergame.com/

Breakdown the problem

  1. Start with the easiest
  2. Turn the problem into comments
  3. Write code → Run code → Fix code
  4. Next Task。

個人需要改進的項目

  1. 隨機有可能重複(沒考慮到)
  2. 重複做的事情,寫成Function
  3. 輸入Guess的大小寫
from art import logo,vs
from replit import clear
import random
from game_data import data
#1.show logo and vs
#2.create 2 dict compareA and againstB
#3.create a function to get a random record
#4.get correct answer
#5.compare function to check which one is correct
#6.moving againstB to compareA if correct 
#7.clear()
#8.show score

score = 0
def getData():
  """隨機取得一個資料"""
  return random.choice(data)
def compare(compareA,AgainstB):
  """取得正確答案"""
  if compareA['follower_count'] > AgainstB['follower_count']:
    return "A"
  else:
    return "B"
def formatPrint(data):
  """格式化產出列印"""
  return f"{data['name']}, a {data['description']}, from {data['country']}"
  
compareA = getData()
againstB = getData()
is_continue = True
print(logo)
while is_continue:
  print(f"Compare A: {formatPrint(compareA)}")
  print(vs)
  print(f"Against B:  {formatPrint(againstB)}")
  guess  = input("Who has more followers? Type 'A' or 'B': ")
  answer = compare(compareA,againstB)
  clear()
  print(logo)
  if guess ==answer:
    score +=1
    compareA = againstB
    againstB = getData()    
    print(f"You're right! Current score: {score}.")
  else:
    print(f"Sorry, that's wrong. Final score: {score}.")
    is_continue = False

講師的Code

Untitled

def check_answer(guess, a_followers, b_followers):
  if a_followers > b_followers:
    return guess == "a"
  else:
    return guess == "b"
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()

'''

FAQ: Why does choice B always become choice A in every round, even when A had more followers? 

Suppose you just started the game and you are comparing the followers of A - Instagram (364k) to B - Selena Gomez (174k). Instagram has more followers, so choice A is correct. However, the subsequent comparison should be between Selena Gomez (the new A) and someone else. The reason is that everything in our list has fewer followers than Instagram. If we were to keep Instagram as part of the comparison (as choice A) then Instagram would stay there for the rest of the game. This would be quite boring. By swapping choice B for A each round, we avoid a situation where the number of followers of choice A keeps going up over the course of the game. Hope that makes sense :-)

'''

# Generate a random account from the game data.

# Format account data into printable format.

# Ask user for a guess.

# Check if user is correct.
## Get follower count.
## If Statement

# Feedback.

# Score Keeping.

# Make game repeatable.

# Make B become the next A.

# Add art.

# Clear screen between rounds.