【发布时间】:2017-05-25 12:50:30
【问题描述】:
在课堂上,我们正在研究计算正方形或矩形面积的函数。该程序询问一个人的姓名,他们想要什么形状以及长度和宽度是什么。然后它打印该形状的区域,程序再次循环回来。我要做的是获取每个单独的名称输入和区域并将它们输出到文本文件中。我们的老师没有说得太清楚如何做到这一点。任何帮助,将不胜感激。代码如下:
import time
def area(l, w):
area = l * w
return area
def square():
width = int(input("please enter the width of the square"))
squareArea = area(width, width)
return squareArea
def rectangle():
width = int(input("please enter the width of the rectangle"))
length = int(input("please enter the length of the rectangle"))
rectangleArea = area(length, width)
return rectangleArea
def main():
name = input("please enter your name")
shape = input("please enter s(square) or r(rectangle)")
if shape == "r" or shape =="R":
print ("area =", rectangle())
main()
elif shape == "s" or shape == "S":
print ("area =", square())
main()
else:
print ("please try again")
main()
main()
编辑:我觉得我问的问题不够清楚,抱歉。我希望能够输入一些东西,例如名称并能够将其放入文本文件中。
【问题讨论】:
-
您想从一个文本文件获取输入,还是希望将输出发送到一个文本文件?
-
将输出发送到文本文件