问题描述
所以我有这个简短的程序,应该给出形状的区域。
当我给它赋值给which_area(x)
时,它可以正常工作。
但是,当我将raw_input()
作为参数时,(并且因为我将print choose_area
放在了底部),shell在输入后回复None
。
我不太确定出了什么问题,但是我认为这与return
语句以及我的表达方式有关。
任何帮助表示赞赏:)请,谢谢。
从一开始的程序员。
def triangle_area():
print "What is the base length?"
base = raw_input()
print "What is the height?"
height = raw_input()
area = 0.5*float(base)*float(height)
print "The area of you triangle is:", area
def circle_area():
print "What is the radius of the circle?"
radius = raw_input()
area = 3.14159*(float(radius)**2)
print "The area of your circle is:", area
def square_area():
print "What is the length of the side of the square?"
print "(Remember that squares have equal sides, and if"
print "you want to enter a seperate length and width"
print "you have a rectangle.)"
length = raw_input()
area = float(length)**2
print "The area of your square is", area
def which_area(x):
if x == 1:
return triangle_area()
elif x == 2:
return circle_area()
elif x == 3:
return square_area()
else:
return "You didn't pick 1, 2, or 3."
def choose_area():
print "Calculate the area of which shape?"
print "1. a triangle"
print "2. a circle"
print "3. a square"
print "Enter a number to choose:"
which_area(raw_input())
print choose_area()
1楼
choose_area
需要返回一个值。
仅计算面积是不够的。
更改which_area(raw_input())
中的最后一行choose_area
到return which_area(raw_input())
2楼
更改您的代码
which_area(raw_input())
至
which_area(int(raw_input()))
3楼
可能有几个原因取决于您使用的python版本。
如果使用的是python版本3或更高版本,则由于python版本错误而导致。 它应该变成
which_area(int(input()))
或者,如果您将波纹管版本用于python 3.x,则应为(例如-2.7)
which_area(int(raw_input()))
无论如何,请确保使用强制转换,因为您正在使用输入来输入数字,但是
raw_input() or input()
函数将输入作为字符串
因此,根据您的计算,如果要获取小数,请使用“ Float”,如果要获取数字,请使用“ int”作为强制转换。
4楼
结合以前的答案:
which_area
需要一个整数, raw_input()
返回一个字符串。
你需要:
which_area(int(raw_input()))
但是,连同此triangle_area
, circle_area
和square_area
均不返回任何内容-而是打印一条消息。
您可能想以以下形式结束这些功能:
return "The area of your triangle is: " + str(area)
(请注意,我在这里仅使用了简单的字符串连接-您可以改用旧样式或新样式的字符串格式)
5楼
函数triangle_area,square_area和circle_area没有返回任何值
def triangle_area():
print "What is the base length?"
base = raw_input()
print "What is the height?"
height = raw_input()
area = 0.5*float(base)*float(height)
print "The area of you triangle is:", area
return area
def circle_area():
print "What is the radius of the circle?"
radius = raw_input()
area = 3.14159*(float(radius)**2)
print "The area of your circle is:", area
return area
def square_area():
print "What is the length of the side of the square?"
print "(Remember that squares have equal sides, and if"
print "you want to enter a seperate length and width"
print "you have a rectangle.)"
length = raw_input()
area = float(length)**2
print "The area of your square is", area
return area
def which_area(x):
if x == 1:
return triangle_area()
elif x == 2:
return circle_area()
elif x == 3:
return square_area()
else:
return "You didn't pick 1, 2, or 3."
def choose_area():
print "Calculate the area of which shape?"
print "1. a triangle"
print "2. a circle"
print "3. a square"
print "Enter a number to choose:"
which_area(int(raw_input()))
print choose_area()
注意:raw_input以promt作为参数,因此您可以执行raw_input(“ Height:”)之类的操作