当我将raw_input()作为参数时,为什么这个函数不起作用?

时间:2014-03-14 10:18:42

标签: python return nonetype

所以我有这个短程序,应该给出一个形状的区域。 当我在调用它时给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()

5 个答案:

答案 0 :(得分:0)

choose_area需要返回一个值。简单计算面积是不够的。

which_area(raw_input())的最后一行choose_area更改为return which_area(raw_input())

答案 1 :(得分:0)

更改您的代码

which_area(raw_input())

which_area(int(raw_input()))

答案 2 :(得分:0)

取决于您使用的python版本,原因很少。

  1. 由于错误的python版本 如果您使用的是python版本3或更高版本。它应该改为

    which_area(int(input()))
    
  2. 或者如果你使用bellow版本到python 3.x那么它应该是(ex-2.7)

       which_area(int(raw_input()))
    

    无论如何要确保使用强制转换,因为你使用输入来取一个数字但是

       raw_input() or input()
    

    函数将输入作为String

    因此,根据您的计算,如果您想获得小数,请使用“Float”,如果您想获得哪些数字使用“int”作为演员。

答案 3 :(得分:0)

结合以前的答案:

which_area需要一个整数,raw_input()返回一个字符串。你需要:

    which_area(int(raw_input()))

但是,除了triangle_areacircle_areasquare_area不返回任何内容之外,他们还会打印一条消息。您可能希望使用以下内容结束这些功能:

    return "The area of your triangle is: " + str(area)

(请注意,我刚刚使用了简单的字符串连接 - 您可以使用旧式或新式字符串格式代替)

答案 4 :(得分:0)

函数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:”)