I am trying to change the code that it can pick the correct guess as "CompareA" and pick another random choice for "AgainstB", not only switch B to A

Question:

It is a guessing game where the computer picks 2 random choices and the user guesses which one is correct. If the guess is right, the second choice (AgainstB) will be replaced with the first position (CompareA) of random choice, and the computer picks another for the second position. I am trying to change the way that the correct answer goes to the first position, not the second choice all the time.
Below is the code:

data = [
    {
        'name': 'Instagram',
        'follower_count': 346,
        'description': 'Social media platform',
        'country': 'United States'
    },
    {
        'name': 'Cristiano Ronaldo',
        'follower_count': 215,
        'description': 'Footballer',
        'country': 'Portugal'
    },
    {
        'name': 'Ariana Grande',
        'follower_count': 183,
        'description': 'Musician and actress',
        'country': 'United States'
    },
    {
        'name': 'Dwayne Johnson',
        'follower_count': 181,
        'description': 'Actor and professional wrestler',
        'country': 'United States'
    },
    {
        'name': 'Selena Gomez',
        'follower_count': 174,
        'description': 'Musician and actress',
        'country': 'United States'
    },
    {
        'name': 'Kylie Jenner',
        'follower_count': 172,
        'description': 'Reality TV personality and businesswoman and Self-Made Billionaire',
        'country': 'United States'
    },
    {
        'name': 'Kim Kardashian',
        'follower_count': 167,
        'description': 'Reality TV personality and businesswoman',
        'country': 'United States'
    }
]

import random
score=0

def account_format(account):
    """Format account into printable format: name, description and country"""
    account_name = account["name"]
    account_description = account["description"]
    account_country = account["country"]
    return f"{account_name}, a {account_description} form {account_country}"

def check_followers(guess, accountA_followers, accountB_followers,):
    """Checks followers against user's guess
      and returns True if they got it right.
      Or False if they got it wrong."""
    if accountA_followers > accountB_followers:
       return guess=="a"
    elif accountA_followers < accountB_followers:
       return guess=="b"

end_of_game=False
accountA=random.choice(data)
while not end_of_game:
    accountA=accountB
    accountB=random.choice(data)

    while accountA == accountB:
        accountB = random.choice(data)

    print(f"CompareA: {account_format(accountA)}.")
    print(f"AgainstB: {account_format(accountB)}.")

    accountA_followers=accountA["follower_count"]
    accountB_followers=accountB["follower_count"]
    print(f"{accountA_followers}, {accountB_followers}")
    guess=input("Who has more followers? Type 'a' or 'b': ").lower()

    is_correct=check_followers(guess, accountA_followers, accountB_followers,)
    if is_correct:
       score+=1
       print(f"You are right, current score: {score}")
    else:
       end_of_game=True
       print(f"Sorry, You are wrong, final score: {score}")

Asked By: Siamak

||

Answers:

end_of_game = False
accountA = random.choice(data)
while not end_of_game:
    accountB = random.choice(data)

    while accountA == accountB:
        accountB = random.choice(data)

    print(f"CompareA: {account_format(accountA)}.")
    print(f"AgainstB: {account_format(accountB)}.")

    accountA_followers = accountA["follower_count"]
    accountB_followers = accountB["follower_count"]
    print(f"{accountA_followers}, {accountB_followers}")
    guess = input("Who has more followers? Type 'a' or 'b': ").lower()

    is_correct = check_followers(guess, accountA_followers, accountB_followers, )
    if is_correct:
        if guess == "b":
            accountA = accountB
        score += 1
        print(f"You are right, current score: {score}")
    else:
        end_of_game = True
        print(f"Sorry, You are wrong, final score: {score}")

This way if the guess is correct accountA remains the same if a was correct or changes to accountB if b was correct option.

Answered By: Vulwsztyn

You only need the accountB assignment on the first line of the while loop, you just have to initialize accountA before the loop. (Your code breaks right now because accountB is not initialized)

If the guess is correct, you just test if guess is ‘b’ and store accountB in accountA if it is true.
By the way: I added os.system(‘cls’ if os.name == ‘nt’ else ‘clear’)
Try it out, this will clear the console if the answer was right, so the screen doesn’t get cluttered up

I hope this helps

end_of_game = False
accountA = random.choice(data)
while not end_of_game:
    accountB = random.choice(data)
    while accountA == accountB:
        accountB = random.choice(data)

    print(f"CompareA: {account_format(accountA)}.")
    print(f"AgainstB: {account_format(accountB)}.")

    accountA_followers = accountA["follower_count"]
    accountB_followers = accountB["follower_count"]
    print(f"{accountA_followers}, {accountB_followers}")
    guess = input("Who has more followers? Type 'a' or 'b': ").lower()

    is_correct = check_followers(guess, accountA_followers, accountB_followers)

    if is_correct:
        if guess == 'b':
            accountA = accountB
        os.system('cls' if os.name == 'nt' else 'clear')
        score += 1
        print(f"You are right, current score: {score}")
    else:
        end_of_game = True
        print(f"Sorry, You are wrong, final score: {score}")
Answered By: Ovski
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.