作者bottomless (碎)
看板Python
标题[问题] 新手试写函式却卡住了
时间Sun Jul 17 08:48:38 2016
小弟是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://webptt.com/cn.aspx?n=bbs/Python/M.1468716526.A.7BF.html
1F:推 eight0: 变数名称 和 字串 是不同的东西,你把两者搞混了 07/17 09:11
2F:→ bottomless: 不太懂…james代表的不是变数名称吗?! 07/17 09:44
3F:推 Raymond0710: james.txt 和 "james.txt" 先了解这两个差别 07/17 11:08
9F:→ kevinkung: 这样写就可以了 07/17 14:19
10F:→ bottomless: 多谢K大的帮忙…太感谢了… 07/17 14:42
11F:→ bottomless: 一、三楼的e大跟R大…也谢谢两位抽空帮忙…^^ 07/17 14:44