[英]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.我有一个包含数据的
opponent1
, opponent2
opponent1_flwrs
的opponent2_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.