我从学习Python艰难的方式学习python我想制作一个我自己的程序,它结合了我迄今学到的一切。我似乎无法弄清楚回报是如何运作的。我的程序看起来像这样:
#Volume for rectangular prism
def rect_prism_vol(x, y, z):
return x*y*z
#Volume for trangular prism
def tri_prism_vol(a, c, h):
return (a*c*h)/2
#Creates txt file stating the volumes
def mk_file(input_file):
txt = open(input_file, 'wr+')
txt.write("""
The volume of a rectangular prism with the dimensions you gave me is %d cubic meters.
The volume of a triangular prism with the dimensions you gave me is %d cubic meters.
""") % (r_vol, t_vol)
#Prompt user for dimensions of rectangular prism
x = float(raw_input("Dimensions of rectangle\n>"))
y = float(raw_input(">"))
z = float(raw_input(">"))
r_vol = rect_prism_vol(x, y, z)
#Prompt user for dimensions of triangular prism
a = float(raw_input("Dimensions of triangle\n>"))
c = float(raw_input(">"))
h = float(raw_input(">"))
t_vol = tri_prism_vol(a, c, h)
#So I can see the results of the formulas
print t_vol
print r_vol
#Make the file with the name below
mk_file("one.txt")
#See what was printed in the file
print txt.read()
#Close file
txt.close()
这给了我这个错误信息,我似乎无法弄清楚如何修复。
Traceback (most recent call last):
File "practice.py", line 27, in <module>
mk_file("one.txt")
File "practice.py", line 12, in mk_file
""") % (r_vol, t_vol)
TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
非常感谢任何帮助。
答案 0 :(得分:-1)
)
位置错误
现在你有了
write("text") % (r_vol, t_vol)
但你需要
write( "text" % (r_vol, t_vol) )
它像这样工作
new_text = "text" % (r_vol, t_vol)
write( new_text )