Home > Back-end >  Calling a variable from a nested function python
Calling a variable from a nested function python

Time:01-08

Idea of the project is

  1. Make random selection A and random selection B

  2. Compare the follower_count from both of these data

  3. If the answer is correct ..replace random selection A with the correct answer data and generate another random selection B (so if A is correct that means no need to change, only B need to change; and if B is correct it needs to replace and become the new A, generate a new B) . Repeat until answer is incorrect. stuck at the def game(). Help appreciated

     from art import logo, vs
     from data import data
     import random
    
     def selection_a():
         a = random.choice(data)
         print(f"Compare A: {a.get('name')}, a {a.get('description')}, from 
         {a.get('country')}")
    
         def followers_a():
             num_of_followers_a = a.get('follower_count')
             print(num_of_followers_a)
             return num_of_followers_a
         followers_a()
         return a
    
     def selection_b():
         b = random.choice(data)
         print(f"Against B: {b.get('name')}, a {b.get('description')}, from 
         {b.get('country')}")
    
         def followers_b():
              num_of_followers_b = b.get('follower_count')
             print(num_of_followers_b)
             return num_of_followers_b
         followers_b()
         return b    
    
      def game():
            end_game = False
    
            selection_a()
            print(vs)
            selection_b()
    
            followers_a
            followers_b
    
            while not end_game:
    
                 answer = input("Who has more followers? Type 'A' or 'B': ").upper()
    
                 current_score = 0
    
                 if answer == "A":
                    if followers_a > followers_b:
                       current_score  = 1
                       print(f"You are right! Current score: {current_score}")
                       selection_a()
    
                    else:
                       print(f"Sorry, that's wrong, Final score: {current_score}")
                        end_game = True
    
                elif answer == "B":
                     if followers_b > followers_a:
                         current_score  = 1
                         print(f"You are right! Current score: {current_score}")
                         selection_b()
                     else:
                        print(f"Sorry, that's wrong, Final score: {current_score}")
                        end_game = True
    
                else:
                   print("invalid entry, type again")
    
     game()
    

CodePudding user response:

You should first set the results of your functions to variables.


    selected_data_a = selection_a()
    selected_data_b = selection_b()
    followers_a = selected_data_a.get('follower_count')
    followers_b = selected_data_b.get('follower_count')

  •  Tags:  
  • Related