Python代码运行解决思路
Python代码运行
import os
os.getcwd()
os.chdir('C:\Python34\HeadFirstPython\chapter3')
man = []
other = []
try:
data = open('sketch.txt')
for each_line in data:
try:
(role, line_spoken) = each_line.split(':', 1)
line_spoken = line_spoken.strip()
if role == 'Man' :
man.append(line_spoken)
elif role == 'Other Man' :
other.append(line_spoken)
except ValueError:
pass
data.close()
except IOError:
print('The datafile is missing!')
print('man')
print('other')
这个和书本一样的代码 为什么不能运行啊 显示语法错误
------解决思路----------------------
3楼的方法是可以的。
尝试下面方法也可以:
如果是语法错误的话,是不是缩进上有问题?感觉你贴的代码格式上有些乱,直接运行会出错
import os
os.getcwd()
os.chdir('C:\Python34\HeadFirstPython\chapter3')
man = []
other = []
try:
data = open('sketch.txt')
for each_line in data:
try:
(role, line_spoken) = each_line.split(':', 1)
line_spoken = line_spoken.strip()
if role == 'Man' :
man.append(line_spoken)
elif role == 'Other Man' :
other.append(line_spoken)
except ValueError:
pass
data.close()
except IOError:
print('The datafile is missing!')
print('man')
print('other')
这个和书本一样的代码 为什么不能运行啊 显示语法错误
------解决思路----------------------
3楼的方法是可以的。
尝试下面方法也可以:
os.chdir('C:\\Python34\\HeadFirstPython\\chapter3')
如果是语法错误的话,是不是缩进上有问题?感觉你贴的代码格式上有些乱,直接运行会出错
import os
os.getcwd()
os.chdir('C:\\Python34\\HeadFirstPython\\chapter3')
man = []
other = []
try:
data = open('sketch.txt')
for each_line in data:
try:
(role, line_spoken) = each_line.split(':', 1)
line_spoken = line_spoken.strip()
if role == 'Man' :
man.append(line_spoken)
elif role == 'Other Man' :
other.append(line_spoken)
except ValueError:
pass
data.close()
except IOError:
print('The datafile is missing!')
print('man')
print('other')