简体   繁体   English

如何在另一个 function 的输入中使用?

[英]How do I use in input from one function in another?

So I'm writing a higher or lower game and I am experimenting with functions.所以我正在写一个更高或更低的游戏,我正在试验功能。 I have a couple functions in my code but I need 2 in particular to kind of work with each other in order for my code to run properly (my code isn't finished yet so there are still a lot of mechanics I need to add).我的代码中有几个函数,但我特别需要 2 个函数才能相互协作,以便我的代码正常运行(我的代码还没有完成,所以我仍然需要添加很多机制) .

def winner(opponent1, opponent2):

  opponent1_flwrs = opponent1['follower_count']
  opponent2_flwrs = opponent2['follower_count']
  score = 0

  if opponent1_flwrs > opponent2_flwrs:
    if higher_or_lower == "A" or higher_or_lower == "a":
      score += 1
      return "You're right! Current score: " + score
    else:
      clear()
      print(art.logo)
      return "Sorry, that's wrong. Final score: " + score
  elif opponent2_flwrs > opponent1_flwrs:
    if higher_or_lower == "B" or higher_or_lower == "b":
      score += 1
      return "You're right! Current score: " + score
    else:
      clear()
      print(art.logo)
      return "Sorry, that's wrong. Final score: " + score

def game(opponent1, opponent2:
  """
  Contains the code necessary for the game to begin and will continue to execute 
  as long as the user is the winner
  """
  print(art.logo)
  print(f"Compare A: {opponent1['name']}, a {opponent1['description']}, "
        f"from {opponent1['country']}")
  print(art.vs)
  print(f"Against B: {opponent2['name']}, a {opponent2['description']}, "
        f"from {opponent2['country']}")
  higher_or_lower = input("Who has more followers A or B? ")

I have a file that contains the data so the values for opponent1 , opponent2 , and opponent1_flwrs , opponent2_flwrs are taken from that data.我有一个包含数据的opponent1opponent2 opponent1_flwrsopponent2_flwrs是从该数据中获取的。

I want the higher_or_lower input from the game function to be able to do the same thing in the winner function and have the same value if that makes sense.我希望游戏 function 中的higher_or_lower输入能够在获胜者 function 中做同样的事情,并且如果有意义的话,它具有相同的值。 Also later on I'm going to add a while loop that's going to call on these functions continuously until the user has lost.稍后我将添加一个while循环,该循环将持续调用这些函数,直到用户丢失。

firstable, sorry for the bad English, but I think that you can just pass the higher_or_lower variable to the winner function like this:首先,抱歉英语不好,但我认为您可以像这样将higher_or_lower变量传递给获胜者function:

def winner(opponent1, opponent2, chosen_option, current_score):
  chosen_option= chosen_option== 'a'
  correct_option= opponent1['follower_count']> opponent2['follower_count']
  
  if chosen_option== correct_option:
    score += 1
    return "You're right! Current score: " + score
  else:
    clear()
    print(art.logo)
    return "Sorry, that's wrong. Final score: " + score

def game(opponent1, opponent2, score):
  """
  Contains the code necessary for the game to begin and will continue to execute as long as the user is the winner
  """
  print(art.logo)
  print(f"Compare A: {opponent1['name']}, a {opponent1['description']}, from {opponent1['country']}")
  print(art.vs)
  print(f"Against B: {opponent2['name']}, a {opponent2['description']}, from {opponent2['country']}")
  higher_or_lower = input("Who has more followers, A or B? ").lower()
  winner(opponent1, opponent2, higher_or_lower)

in the main loop you have to declare the score variable so you don't reset it every time you call the function在主循环中,您必须声明 score 变量,因此每次调用 function 时都不会重置它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在另一个函数中使用来自一个函数的输入? - How do I use input from one function in a different function? 如何从一个函数中获取一个值并在另一个函数中使用它? - How do I take a value from one function and use it in another? 如何将输入变量从一个 function 传递到另一个,而无需多次请求输入? - How do I passing an input variable from one function to another without it asking for the input multiple times? 如何在同一类的另一个函数中使用一个函数中的一个对象 - How do I use one object from one function in another function in the same class 如何获取一个程序的输出并将其用作另一个程序的输入? - How do I take the output of one program and use it as the input of another? 如何在另一个函数中使用一个函数收集的数据 - How do I use the data collected of one function in another function 如何将一个 function 的 output 用于另一个 function? - How do I use the output of one function for another function? 如何在python中的另一个函数中使用一个函数中的数据? - How do I use data from one function in another function in python? 如何在另一个 function 中使用来自一个 function 的本地定义变量? - How do I use a locally defined variable from one function in another function? 如何将变量从一个函数发送到另一个函数? - How do I send a variable from one function to another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM