是否可以减去两个函数的收益?

时间:2019-06-30 05:49:11

标签: python json function

这是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)

2 个答案:

答案 0 :(得分:1)

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

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)

答案 1 :(得分:0)

感谢Aldric的帮助,奥斯汀意识到我犯的错误……初学者的东西。


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_1远高于player_1时,它将返回一个负整数,并且清理代码,因为其中有些与我无关来自奥斯汀,只要我能将它们全部使用同一功能。但是至少我是半目的地做到了! :)