从嵌套函数调用变量 python
Calling a variable from a nested function python
项目的想法是
随机选择A和随机选择B
比较这两个数据的follower_count
如果答案正确..用正确的答案数据替换随机选择A并生成另一个随机选择B(所以如果A正确意味着不需要改变,只有B需要改变; 如果 B 是正确的,它需要替换并成为新的 A,生成新的 B) 。重复直到答案不正确。
卡在 def game()。帮助赞赏
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()
您应该首先将函数的结果设置为变量。
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')
项目的想法是
随机选择A和随机选择B
比较这两个数据的follower_count
如果答案正确..用正确的答案数据替换随机选择A并生成另一个随机选择B(所以如果A正确意味着不需要改变,只有B需要改变; 如果 B 是正确的,它需要替换并成为新的 A,生成新的 B) 。重复直到答案不正确。 卡在 def game()。帮助赞赏
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()
您应该首先将函数的结果设置为变量。
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')