我对 Python 完全陌生,我只是在练习和“复制”一些 github 代码。在这样做的过程中,我意识到我正在使用最新版本的Python,而视频中的人正在使用Python 2.7,因此他使用的代码与我的不兼容。请帮助我如何重写代码。
def add_friend(self,friend_id):
check_req = requests.get('https://gift-f06e4.firebaseio.com/.json?orderBy="my_friend_id"&equalTo=' + friend_id)
data = check_req.json()
print(check_req.ok)
print(check_req.json())
if data == {}:
self.root.ids['add_friend_screen'].ids['add_friend_label'].text = "Invalid friend ID"
else:
key = data.keys()[0]
new_friend_id = data[key]['my_friend_id']
print('New friend id is', new_friend_id)
Run Code Online (Sandbox Code Playgroud)
问题出现在这一行:
key = data.keys()[0]
Run Code Online (Sandbox Code Playgroud)
我知道这不是我在 Python 3 中编写这部分的方式,但我不知道如何让它工作。