Dalam kode saya untuk simulasi NBA yang memungkinkan pengguna untuk memilih 2 tim dari musim yang sama atau berbeda. Jika pengguna memilih kedua tim dari musim 2019-20 maka array 1 poin tim dan array 1 poin lawan terisi tetapi array tim 2 dan 2 lawan kosong. Setiap kombinasi musim dan tim lainnya bekerja mis. tim 1 dari 2019-20 dan tim 2 dari 2015-16. Saya cukup yakin masalahnya berkaitan dengan loop for dalam pernyataan if yang memeriksa apakah season2 sama dengan 2019-20.

import csv
from nba_api.stats.static import teams
import random as rnd
import numpy as np

team1_search = teams.find_teams_by_full_name('Bucks')
team1_abbr = team1_search[0]['abbreviation']

team2_search = teams.find_teams_by_full_name('Lakers')
team2_abbr = team2_search[0]['abbreviation']

with open('nba_stats/stats/data/1949-2018_officialBoxScore.csv', 'r') as csv_file:
        csv_dict_reader = csv.DictReader(csv_file)

        seasons = []
        for row in csv_dict_reader:
            seasons.append(row['season'])

        set_res = set(seasons)
        seasons = (list(set_res))

season1 = '2019-20'
season2 = '2019-20'

if season1 == '2019-20' or season2 == '2019-20':
    with open('nba_stats/stats/data/2019-20_officialBoxScore.csv', 'r') as csv_file:
        csv_dict_reader = csv.DictReader(csv_file)

        if season1 == '2019-20':
            team1PTS = []
            oppt1PTS = []
            for row in csv_dict_reader:         
                if row['teamAbbr'] == team1_abbr and row['seasonType'] == 'Regular':
                    team1PTS.append(float(row['teamPTS']))
                    oppt1PTS.append(float(row['opptPTS']))
                        
                elif row['opptAbbr'] == team1_abbr and row['seasonType'] == 'Regular':
                    team1PTS.append(float(row['opptPTS']))
                    oppt1PTS.append(float(row['teamPTS']))

        if season2 == '2019-20':
            team2PTS = []
            oppt2PTS = []
            #I'm pretty sure this for loop is the issue after a couple of tests
            for row in csv_dict_reader:   
                if row['teamAbbr'] == team2_abbr and row['seasonType'] == 'Regular':
                    team2PTS.append(float(row['teamPTS']))
                    oppt2PTS.append(float(row['opptPTS']))

                elif row['opptAbbr'] == team2_abbr and row['seasonType'] == 'Regular':
                    team2PTS.append(float(row['opptPTS']))
                    oppt2PTS.append(float(row['teamPTS']))

Perilaku yang diinginkan

Bucks: 98
Lakers: 117

Kesalahan

Warning: Mean of empty slice.
  return _methods._mean(a, axis=axis, dtype=dtype,
C:\Users\panos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\numpy\core\_methods.py:161: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
C:\Users\panos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\numpy\core\_methods.py:216: RuntimeWarning: Degrees of freedom <= 0 for slice
  ret = _var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
C:\Users\panos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\numpy\core\_methods.py:185: RuntimeWarning: invalid value encountered in true_divide
LocalCache\local-packages\Python38\site-packages\numpy\core\_methods.py:209: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
e 106, in <module>
    team1_sim_pts = int(round((rnd.gauss(team1_mean, team1_std) + rnd.gauss(opp2_mean, opp2_std))) / 2)
-2
panosp1705 2 September 2020, 23:30

1 menjawab

Jawaban Terbaik

Setelah loop for pertama, semua yang ada di file telah dibaca, jadi tidak ada yang tersisa untuk dibaca oleh loop kedua. Memulai loop for baru tidak akan mereset file kembali ke awal. Anda perlu mencari kembali ke awal file sebelum mencoba membacanya lagi.

        if season2 == '2019-20':
            team2PTS = []
            oppt2PTS = []
            csv_file.seek(0) # rewind to beginning of file
            for row in csv_dict_reader:   
                if row['teamAbbr'] == team2_abbr and row['seasonType'] == 'Regular':
                    team2PTS.append(float(row['teamPTS']))
                    oppt2PTS.append(float(row['opptPTS']))

                elif row['opptAbbr'] == team2_abbr and row['seasonType'] == 'Regular':
                    team2PTS.append(float(row['opptPTS']))
                    oppt2PTS.append(float(row['teamPTS']))
0
Barmar 2 September 2020, 20:37