这里是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)发布于 2019-06-30 14:21:35
当你调用一个函数并返回结果时,你可以将它保存到一个新的变量中以供以后使用:
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)发布于 2019-06-30 14:17:01
感谢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高得多时,它会返回一个负整数,并清理代码,因为其中一些与我从奥斯汀获得的代码相比是毫无意义的,只要我可以将它们都放在同一个函数中。但至少--我把它变成了自己的目标!:)
https://stackoverflow.com/questions/56822701
复制相似问题