看板 Python 關於我們 聯絡資訊
小弟是python的超級新手... 目前正在練O`REILLY的深入淺出python… 不過到第五章就卡關了…想問一下到底錯在哪… 書上是要處理運動員的跑步成績… 以下先提供書上的程式碼: with open("james.txt") as jaf: data = jaf.readline() james = data.strip().split(",") with open("julie.txt") as juf: data = juf.readline() julie = data.strip().split(",") with open("mikey.txt") as mif: data = mif.readline() mikey = data.strip().split(",") with open("sarah.txt") as saf: data = saf.readline() sarah = data.strip().split(",") print(james) print(julie) print(mikey) print(sarah) 那小弟的想法是用函式寫寫看… 以下是小弟寫函式的程式碼: def personal_time(the_list, middle, the_name): try: with open("the_list") as middle: data = middle.readline() the_name = data.strip().split(",") print(the_name) except IOError as err: print("File Error : " + str(err)) 接著再用另一個py來輸出 import def_time personal_time(james.txt, jaf, james) 但是它一直跳出 Traceback (most recent call last): File "personal_time.py", line 11, in <module> personal_time(james.txt, jaf, james) NameError: name 'james' is not defined 小弟知道它是說james沒有定義… 但是要怎麼改才對?! 還是說有哪方面的資料可以參考… 小弟完全沒有頭緒要從什麼地方改啊…>"< -- ██ █ █ █◤ ◢◣ bottomless -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.42.163.252 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1468716526.A.7BF.html
eight0: 變數名稱 和 字串 是不同的東西,你把兩者搞混了 07/17 09:11
bottomless: 不太懂…james代表的不是變數名稱嗎?! 07/17 09:44
Raymond0710: james.txt 和 "james.txt" 先了解這兩個差別 07/17 11:08
kevinkung: /GzYc9dP.jpg http://i.imgur.com/Kov3rEt.jpg 07/17 14:17
kevinkung: http://i.imgur.com/1rX6MVx.jpg 07/17 14:18
kevinkung: http://i.imgur.com/ai5NthE.jpg 07/17 14:18
kevinkung: http://i.imgur.com/0nJOak2.jpg 07/17 14:19
kevinkung: 這樣寫就可以了 07/17 14:19
bottomless: 多謝K大的幫忙…太感謝了… 07/17 14:42
bottomless: 一、三樓的e大跟R大…也謝謝兩位抽空幫忙…^^ 07/17 14:44