第一个python项目和can';我忍不住想';我使用了很多不必要的编码

第一个python项目和can';我忍不住想';我使用了很多不必要的编码,python,api,riot,Python,Api,Riot,这是我的第一个编码项目,目标也是使用RiotAPI中的数据评估传奇联盟中的球员技能。通过增加一个等级系统来确定一个球员有多优秀,我几乎就要完成它了。在我编写代码的过程中,我觉得我编写了很多不必要的代码,这些代码可以很容易地缩短,但是我想不出什么东西来。我可以使用哪些函数或方法来缩短代码?这也是我第一次使用stackoverflow,所以我可能做错了什么 #ASKING USER FOR SUMMONER NAME sumName = input('Enter summoner name:')

这是我的第一个编码项目,目标也是使用RiotAPI中的数据评估传奇联盟中的球员技能。通过增加一个等级系统来确定一个球员有多优秀,我几乎就要完成它了。在我编写代码的过程中,我觉得我编写了很多不必要的代码,这些代码可以很容易地缩短,但是我想不出什么东西来。我可以使用哪些函数或方法来缩短代码?这也是我第一次使用stackoverflow,所以我可能做错了什么

#ASKING USER FOR SUMMONER NAME


sumName = input('Enter summoner name:')


#COLLECTING DATA TO BE INSERTING FOR MATCHLIST DATABASE


url=('https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/'+(sumName)+'?api_key='+(key))


response=requests.get(url)

accId=(response.json()['accountId'])


#COLLECTING DATA FOR THE NEXT DATABASE API


url2=('https://na1.api.riotgames.com/lol/match/v4/matchlists/by-account/'+(accId)+'?queue=420&endIndex=20&api_key='+(key))
response2=requests.get(url2)


i=0
GAMEID = []
Idgame=20

#COLLECTS GAME ID'S FOR NEXT DATABASE FOR 20 GAMES


while Idgame>0:
    GAMEID.append(response2.json()['matches'][i]['gameId'])
    i=i+1
    Idgame=Idgame-1















#COLLECTING DATA FROM GAME 1


class GAME1():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[0])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0


    #THIS COLLECT THE ID NUMBER OF THE PLAYER NAME THAT WAS INSERTED


    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
            
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']






#COLLECTING DATA FROM GAME 2    


class GAME2():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[1])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 3


class GAME3():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[2])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']






#COLLECTING DATA FROM GAME 4


class GAME4():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[3])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 5


class GAME5():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[4])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 6


class GAME6():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[5])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 7


class GAME7():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[6])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 8


class GAME8():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[7])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 9


class GAME9():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[8])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 10


class GAME10():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[9])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']








#COLLECTING DATA FROM GAME 11


class GAME11():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[10])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 12


class GAME12():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[11])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 13


class GAME13():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[12])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 14


class GAME14():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[13])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 15


class GAME15():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[14])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 16


class GAME16():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[15])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 17


class GAME17():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[16])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 18


class GAME18():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[17])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 19


class GAME19():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[18])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 20


class GAME20():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[19])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']
            



#Object from each game class


game1= GAME1()
game2= GAME2()
game3= GAME3()
game4= GAME4()
game5= GAME5()
game6= GAME6()
game7= GAME7()
game8= GAME8()
game9= GAME9()
game10= GAME10()


#Calcuating the average of 10 games for each stat


killsAvg= (game1.kills+game2.kills+game3.kills+game4.kills+game5.kills+game6.kills+game7.kills+game8.kills+game9.kills+game10.kills)/10
assistsAvg=(game1.assists+game2.assists+game3.assists+game4.assists+game5.assists+game6.assists+game7.assists+game8.assists+game9.assists+game10.assists)/10
deathsAvg=(game1.deaths+game2.deaths+game3.deaths+game4.deaths+game5.deaths+game6.deaths+game7.deaths+game8.deaths+game9.deaths+game10.deaths)/10
visionsAvg=(game1.visions+game2.visions+game3.visions+game4.visions+game5.visions+game6.visions+game7.visions+game8.visions+game9.visions+game10.visions)/10
csAvg=(game1.csTotal+game2.csTotal+game3.csTotal+game4.csTotal+game5.csTotal+game6.csTotal+game7.csTotal+game8.csTotal+game9.csTotal+game10.csTotal)/10

print('His average kills is '+str(killsAvg)+' in the last 10 games')
print('His average assists is '+str(assistsAvg)+' in the last 10 games')
print('His average deaths is '+str(deathsAvg)+' in the last 10 games')
print('His average visions is '+str(visionsAvg)+' in the last 10 games')
print('His average csing is '+str(csAvg)+' in the last 10 games')
#询问用户召唤师姓名
sumName=input('输入召唤者名称:')
#正在收集要为匹配列表数据库插入的数据
url=('https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/“+(sumName)+”?api_键=”+(键))
response=requests.get(url)
accId=(response.json()['accountId'])
#为下一个数据库API收集数据
url2=('https://na1.api.riotgames.com/lol/match/v4/matchlists/by-account/“+(accId)+”?queue=420&endIndex=20&api_key=”+(key))
response2=requests.get(url2)
i=0
GAMEID=[]
Idgame=20
#为20个游戏的下一个数据库收集游戏ID
当Idgame>0时:
append(response2.json()['matches'][i]['GAMEID']
i=i+1
Idgame=Idgame-1
#从游戏1收集数据
类GAME1():
url3=('https://na1.api.riotgames.com/lol/match/v4/matches/“+str(GAMEID[0])+”?api_键=”+(键))
response3=requests.get(url3)
循环=0
索引=0
#这将收集插入的播放器名称的ID号

当循环时,您应该学习函数,以便重用代码。例如,请参见以下教程:

函数实际上的工作方式与您使用类的方式非常相似。
但是类的使用方式通常与您使用它们的方式不同,本教程可能会有所帮助

为什么要使用类?为什么会有这么多?基本上,每次在变量名中有序列号时,你都犯了错误。你的问题是什么?这是堆栈溢出。也许你应该试试?