Higher or Lower game
- 라임 샹큼
- Mar 6
- 3 min read
The higher or lower game is a game where the player is asked whether a specific person or thing has more of a quantity such as views, likes etc.
from game_data import data
from art import logo
from art import vs
import random
sum_ = 0
generated_names = []
def generate_name():
global sum_
if sum_ == 0:
for _ in range(2):
generated_names.append(random.choice(data))
else:
for _ in range(1):
generated_names.insert(0,random.choice(data))
def a_or_b():
first_ = 0
second_ = 1
global a
global b
global who_has_more
a = f'{generated_names[first_]['name']}, a {generated_names[first_]['description']}, from {generated_names[first_]['country']}'
b = f'{generated_names[second_]['name']}, a {generated_names[second_]['description']}, from {generated_names[second_]['country']}'
who_has_more = 0
if generated_names[first_]['follower_count'] > generated_names[second_]['follower_count']:
who_has_more = 'a'
elif generated_names[first_]['follower_count'] == generated_names[second_]['follower_count']:
return 0
else:
who_has_more = 'b'
def high_or_low():
print(logo)
global sum_
continual = True
while continual == True:
generate_name()
a_or_b()
print(f'\ncompare A: {a}')
print(vs)
print(f'against B: {b}')
if input('Who has more followers: A or B?\n').lower() == who_has_more:
sum_ += 1
print(f'You\'re right! current score is {sum_}')
randon_num = random.randint(0,1)
generated_names.remove(generated_names[randon_num])
else:
continual = False
print(f'You are wrong. Score was {sum_}')
high_or_low()
The first code looked like this. In this code I tried to use more functions instead of making a very long code and found that it looks more organized and neat. However I regret not using more variable names as the code got longer horizontally and took some time for another person to understand. I also had a problem with making the answer appear on the top after one round.
# Display art
from art import logo, vs
from game_data import data
import random
def format_data(account):
"""Takes the account data and returns the printable format."""
account_name = account["name"]
account_descr = account["description"]
account_country = account["country"]
return f"{account_name}, a {account_descr}, from {account_country}"
def check_answer(user_guess, a_followers, b_followers):
"""Take a user's guess and the follower counts and returns if they got it right."""
if a_followers > b_followers:
return user_guess == "a"
else:
return user_guess == "b"
print(logo)
score = 0
game_should_continue = True
# Generate a random account from the game data
account_b = random.choice(data)
# Make the game repeatable.
while game_should_continue:
# Making account at position B become the next account at position A.
account_a = account_b
account_b = random.choice(data)
if account_a == account_b:
account_b = random.choice(data)
print(f"Compare A: {format_data(account_a)}.")
print(vs)
print(f"Against B: {format_data(account_b)}.")
# Ask user for a guess.
guess = input("Who has more followers? Type 'A' or 'B': ").lower()
# Clear the screen
print("\n" * 20)
print(logo)
# - Get follower count of each account
a_follower_count = account_a["follower_count"]
b_follower_count = account_b["follower_count"]
# Check if user is correct.
is_correct = check_answer(guess, a_follower_count, b_follower_count)
# Give user feedback on their guess.
# score keeping.
if is_correct:
score += 1
print(f"You're right! Current score {score}")
else:
print(f"Sorry, that's wrong. Final score: {score}.")
game_should_continue = False
This was the correct code. By adding hashtags as notes, it was easy to understand the code and the more variable names helped with the understanding. I also realized that I should hashtag the stages I need to go through instead of creating a fully fledged flow chart. It seems more easy and straightforward.
Comments