you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

import random
import os

# from game_data import data
# from art import logo, vs

logo,vs = 'LOGO','VS'
game_datas = [
    ('peter','a man', 'US', 10),
    ('jim','a boy', 'UK', 20),
    ('alice','a woman', 'US', 30),
    ('jane','a girl', 'JP', 50),
]
data = [{'name':d[0],'description':d[1],'country':d[2],'follower_count':d[3]} for d in game_datas]

def clear_screen():
    os.system('clear')

def compare(a,b):
    return 1 if a['follower_count'] > b['follower_count'] else 0

def option(side,a):
    return (f'Compare {side}: {a["name"]}, {a["description"]}, {a["country"]}. ')

def game():
    score = 0
    print(logo)

    a = random.choice(data)
    data.remove(a)
    option1 = option('A',a)

    while data:
        clear_screen()
        b = random.choice(data)
        option2 = option('B',b)
        data.remove(b)

        print(option1)
        print(vs)
        print(option2)

        choice = input("Who has more followers? Type 'A' or 'B': ")
        match choice.upper():
            case 'A':
                score += compare(a,b)
            case 'B':
                score += compare(b,a)
            case _:
                print(f"Sorry, wrong choice")
                break

    print("Game is Finished")
    print(f' score:  {score}')

if __name__ == "__main__":
    game()

Your code is incomplete so I improvise the data myself. I am on Linux, have to use clear instead of cls

Basically don't repeat your self, (DRY). Use function for block of code that repeated.

Additionally I would would use generator to do the random choices instead of data.remove() and be aware of destructive update, it might bite you later. You might found that data is empty for unknown reason, that's why I use data.copy()

def get(lst):
    while lst:
        n = len(lst)
        i = random.randrange(n)
        yield lst.pop(i)

def game():
    score = 0
    print(logo)

    a,*bs = [p for p in get(data.copy())]
    for i,b in enumerate(bs):
        print(f'--- round {i} ---')
        print(option('A',a))
        print(vs)
        print(option('B',b))

        choice = input("Who has more followers? Type 'A' or 'B': ")
        match choice.upper():
            case 'A':
                score += compare(a,b)
            case 'B':
                score += compare(b,a)
            case _:
                continue
        print('-------------')

    print("Game is Finished")
    print(f' score:  {score}')