How to repeat and replace data in a while loop python

Viewed 45

The project generates A and B randomly and compares them, I am able to repeat the loop, however, in the next comparison 1. if A was correct I want it to remain and generate a new B, 2. if B was correct, B becomes the new A and a new B is generated. note: format data is an already defined function not shown

followers_a = account_a.get("follower_count")
followers_b = account_b.get("follower_count") 
end_game = False

while not end_game:
  
  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': ").upper()
  
  current_score = 0

  if guess == "A":
    if followers_a > followers_b:
      current_score += 1
      print(f"You are right! Current score: {current_score}")
    else:
      print(f"Sorry, that's wrong, Final score: {current_score}")
      end_game = True
    
  elif guess == "B":
    if followers_b > followers_a:
      print(f"You are right! Current score: {current_score}")
      current_score += 1 
    else:
      print(f"Sorry, that's wrong, Final score: {current_score}")
      end_game = True
  
  elif guess != "A" or guess != "B":
    print("invalid entry, type again")
1 Answers

That's should do

followers_a = account_a.get("follower_count")
followers_b = account_b.get("follower_count") 
end_game = False

while not end_game:
  
  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': ").upper()
  
  current_score = 0

  if guess == "A":
    if followers_a > followers_b:
      current_score += 1
      print(f"You are right! Current score: {current_score}")
      followers_b = account_b.get("follower_count") 

    else:
      print(f"Sorry, that's wrong, Final score: {current_score}")
      end_game = True
    
  elif guess == "B":
    if followers_b > followers_a:
      print(f"You are right! Current score: {current_score}")
      current_score += 1 
      followers_a = account_a.get("follower_count")

    else:
      print(f"Sorry, that's wrong, Final score: {current_score}")
      end_game = True
  
  elif guess != "A" or guess != "B":
    print("invalid entry, type again")

Related