簡體   English   中英

是否可以減去兩個函數的回報?

[英]Is it possible to subtract the returns of two functions?

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

當您有一個調用並返回結果的函數時,您可以將其保存到一個新變量中以供以后使用:

newVar = function()

這意味着稍后在您的代碼中您可以將其用於其他用途。 此外,當您創建一個函數時,您可以定義在函數中使用的變量,這些變量可以在您調用它時傳遞。 對於您的示例,您可能希望傳遞“用戶名”變量而不是在有效負載中進行輸入。

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

由於我無權訪問您的 API,因此我創建了一個修改后的示例,下面的注釋應該以與您獲得的回報類似的方式做事。 如果您從 API Server 發布示例 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)

感謝 Aldric 的幫助,Austin 意識到了我所犯的錯誤……初學者的東西。


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 它返回一個負整數,並清理代碼,因為我從奧斯汀得到的其中一些是毫無意義的,只要我可以在同一個函數中使用它。 但至少我做到了它的目的! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM