两个函数的返回值可以相减吗?

2024-09-27 04:16:16 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是api的wiki:https://github.com/ppy/osu-api/wiki

所以我建立这个程序来比较等级,所以我抓取用户名和它们的等级,现在我被困在如何存储信息上,然后将两者相减以得到差别!你知道吗

抱歉,我只写了大约2-4周的代码,我肯定这是一个超级简单的东西,我的代码可能是超级恶心。你知道吗

我目前拥有的代码:


def player1():
    payload = {'k': 'myapi', 'u': input()}
    r = requests.get('https://osu.ppy.sh/api/get_user', params=payload)

    player = r.json()[0]
    return (player["pp_country_rank"])
player = player1()
print(player)

print('Enter a second Username')

def player2():
    payload = {'k': 'myapi', 'u': input()}
    r = requests.get('https://osu.ppy.sh/api/get_user', params=payload)

    player = r.json()[0]
    return (player["pp_country_rank"])
player = player2()
print(player)

Tags: 代码httpsapiinputgetdefwikirequests
2条回答

感谢奥尔德里克的帮助,奥斯汀意识到我犯的错误。。。初学者的东西。你知道吗


def player1():
    payload = {'k': '306369747588b5614d76e123cee49dde8f439f7b', 'u': input()}
    r = requests.get('https://osu.ppy.sh/api/get_user', params=payload)

    player_1 = r.json()[0]
    return int(player_1["pp_country_rank"])
player_1 = player1()
print(player_1)

print('Enter a second Username')

def player2():
    payload = {'k': '306369747588b5614d76e123cee49dde8f439f7b', 'u': input()}
    r = requests.get('https://osu.ppy.sh/api/get_user', params=payload)

    player = r.json()[0]
    return int(player["pp_country_rank"])
player = player2()
print(player)

print ('Your difference in rank is:')
output = (player_1 - player)
print (output) 

这就是我的代码现在的样子,我得到的区别是,我有一些小错误,比如每当player\u 1比player高很多的时候,它就会返回一个负整数,清理代码,因为有些代码和我从Austin那里得到的是毫无意义的,每当我可以把它都放在同一个函数中。但至少我是有目的的!:)

当您有一个调用并返回结果的函数时,您可以将其保存到一个新变量中供以后使用:

newVar = function()

这意味着在以后的代码中,您可以将其用于其他用途。另外,当您创建一个函数时,您可以定义在函数中使用的变量,这些变量可以在调用函数时传递。对于您的示例,您可能希望传递“username”变量,而不是在有效负载中进行输入。你知道吗

def myFunc(username):
    print(username)
myFunc(random_user)

因为我没有访问你的API的权限,所以我创建了一个修改过的示例,下面有注释,应该以与你得到的回报类似的方式来做事情。如果您在API服务器上发布一个JSON示例,那么拥有一个工作示例就更容易了。你知道吗

# Temp DB Dictionary for showing how things work
users = {
    "user1": [
        {
            "pp_country_rank": 100,
            "other_data": "random"
        }
    ],
    "user2": [
        {
            "pp_country_rank": 95,
            "other_data": "more-random"
        }
    ],
}

# Defining the function. This can be used over and over.
# In this case there will be a variable created in the
# function called "username". If this isn't passed to the
# function when you call it then it will be set to user1.
# If you do set it, whatever you set it to when you call
# the function will overwrite the default.
def getRank(username="user1"):
    # payload = {'k': 'myapi', 'u': username}
    # r = requests.get('https://osu.ppy.sh/api/get_user', params=payload)
    # Since we created the username variable I can past
    # it to whatever I am calling.
    r = users[username]
    #player = r.json()[0]
    player = r[0]
    # The return here returns the result that gets stored in
    # the variable.
    return (player["pp_country_rank"])

print("Enter first username")
# We are calling the input before passing the result to the
# function
user1 = input()
# We are creating a new variable that will store the result of
# the function getRank(). We pass the stored input of user1 to
# the functuon. In the function that data will be available as
# the variable "username"
user1_rank = getRank(user1)
# There are different ways of formatting and using variables in
# the print. This is one way and where the %s is it will be 
# replaced in order with the variables at the end.
print("%s rank is %s" % (user1, user1_rank))
# We get the second username again storing the input in user2
print('Enter a second Username')
user2 = input()
# We call the same function getRank() but this time we pass the
# data from user2 instead of user1.
user2_rank = getRank(user2)
print("%s rank is %s" % (user2, user2_rank))

# Here we are doing the diff between the two ranks. If you do not
# use abs() then you would have to figure out which rank was bigger
# before doing the substraction to avoid a negative number. This 
# way you will also have a positive diff.
rankDiff = abs(user1_rank - user2_rank)
print("The difference in ranks is %s" % rankDiff)

相关问题 更多 >

    热门问题