Calc. Average of BowlingGame
ボウリングのゲームのアベレージを算出/表示するプログラムを作成してみました。
ブログに表を掲載する場合、ブロック内の表のコピペが簡単にできず、毎回表を作り直すのは面倒なので、せっかくなので、Pythonistaを使い、計算して表示するプログラムを作成しました。
今後は、このプログラムで実行した結果のスクリーンショットを掲載すれば良いようになります。
プログラム作成にあたり、tableviewやCSVファイルの読み書き等を調べながら進めました。
配列の初期化や、初期値の設定方法などもよくわからないことがあったので、いろいろと勉強になりました。
《画面構成》

《実行結果》
※b_score.caveファイルには予め4ゲーム分のデータを入力しておいてから実行しています。
csvファイルは下記の通り。

(1)実行直後画面
下記の通り、4ゲーム分が表示されます。

(2)ゲームスコア追加時の実行画面
※5ゲーム目のスコア「201」を入力し、「Add Data」ボタンを押した時の実行画面です。
5ゲーム目までのスコアが集計され、アベレージまで算出、表示されています。

《ソースコード》
ソースコードは、Pythonistaからコピペした後整形したのでインデントがおかしいところがありますがご了承ください。
デバッグ用にprint文を入れていましたが、そのままにしました。
無駄なコードが多いかもしれませんが、機能的にはこれで十分なので、とりあえずはこれで完成です。
#A program that calculates the average score of bowling games
#Input_file is "b_score.csv" thst csv_file
#Don't add new_line to final_data.
import ui
import csv
new_score = 0
t_sc = 0 #total score
a_sc = 0 #average score
g_num = -1 #title line is not count
G_MAX = 100 #Max. game nums
gn_lst = [0] * G_MAX
sc_lst = [0] * G_MAX
ts_lst = [0] * G_MAX
av_lst = [0] * G_MAX
ret = [('','')] * G_MAX
def readData():
global g_num
global ret
fr = open("b_score.csv") #open file
rp = csv.reader(fr) #for read csv
for rd in rp:
print(rd)
g_num += 1 #increment game num.
if g_num == 0:
gn_lst[0] = "Game"
sc_lst[0] = "Score"
else:
if g_num > G_MAX: #check Max num.(for mem.)
break
if rd == []: #if read blank_line
g_num = g_num - 1 #decrement game-num.
break
print(rd[0])
print(rd[1])
ret[(g_num)*2] = rd[0] #for check
ret[(g_num)*2+1] = rd[1] #for check
gn_lst[g_num] = rd[0] #set game num. list
sc_lst[g_num] = rd[1] #set score list fr.close() return ret
def renewTableView(tv,lst):
lstdata = ui.ListDataSource("") #clear list
tmp_lst = ['0'] * (g_num+1) #malloc table
for i in range(0,g_num+1): #include title
tmp_lst[i] = lst[i] #data copy
lstdata.items = tmp_lst #set items of game number list
tv.data_source = lstdata #set source
tv.reload_data() #display data
#Display game num. and score
def displayGameAndScore():
#Game Number
renewTableView(gn_tv, gn_lst)
renewTableView(sc_tv, sc_lst)
#Display total score and average
def displayAverage():
#calc. total_score and Average
for i in range(0,g_num+1): #to LastData
if i == 0:
ts_lst[0] = "Total" #title is "Total"
av_lst[0] = "Average" #title is "Average"
else:
if i == 1: #first data
ts_lst[1] = sc_lst[1]
av_lst[1] = sc_lst[1] #same top data
else: #2nd.---
ts_lst[i] = str(int(ts_lst[i-1]) + int(sc_lst[i]))
av_lst[i] = str(round(int(ts_lst[i]) / (i)))
#Total Score to tableview
renewTableView(total_tv, ts_lst)
#Average to tableview
renewTableView(ave_tv, av_lst)
print("game_num=",g_num) #for check
def writeData(wd):
global g_num
fw = open('b_score.csv','a')
wp = csv.writer(fw,lineterminator='\n')
ad_data = ['',''] #append data
g_num += 1
gn_lst[g_num] = ad_data[0] = str(g_num)
sc_lst[g_num] = ad_data[1] = wd
ts_lst[g_num] = str(int(ts_lst[g_num-1]) + int(sc_lst[g_num]))
av_lst[g_num] = str(round(int(ts_lst[g_num]) / (g_num)))
renewTableView(gn_tv, gn_lst)
renewTableView(sc_tv, sc_lst)
renewTableView(total_tv, ts_lst)
renewTableView(ave_tv, av_lst)
print("game num.=",g_nu #for check
print("add data=", ad_data[0],ad_data[1]) #for check
wp.writerow(ad_data)
fw.close()
#Add new data proc.
def addData(sender):
nd = nd_tf.text
if nd == '': #check blank
print("Input new score!")
return
if int(nd) == 0: #check zero
print("Input-score is 0!")
return
if int(nd) > 300: #check over 300
print("Input score is over 300")
return
print(nd)
writeData(nd) #display new data and write to file
v = ui.load_view()
v.present('sheet')
nd_tf = v['textfield1'] #text field1 for new data
gn_tv = v['tableview1'] #table_view1 for display game_num
sc_tv = v['tableview2'] #table_view1 for display score
total_tv = v['tableview3'] #table_view1 for display total score
ave_tv = v['tableview4'] #table_view1 for display average
r_data = readData() #read csv-file and set data
print(r_data)
displayGameAndScore() #display game-score to tableview
displayAverage() #display total-score and average to tableview

