当前位置: 代码迷 >> python >> 当我将raw_input()作为参数时,为什么此功能不起作用?
  详细解决方案

当我将raw_input()作为参数时,为什么此功能不起作用?

热度:12   发布时间:2023-07-14 09:48:42.0

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

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

更改which_area(raw_input())中的最后一行choose_areareturn which_area(raw_input())

更改您的代码

which_area(raw_input())

which_area(int(raw_input()))

可能有几个原因取决于您使用的python版本。

  1. 如果使用的是python版本3或更高版本,则由于python版本错误而导致。 它应该变成

     which_area(int(input())) 

或者,如果您将波纹管版本用于python 3.x,则应为(例如-2.7)

   which_area(int(raw_input()))

无论如何,请确保使用强制转换,因为您正在使用输入来输入数字,但是

   raw_input() or input()

函数将输入作为字符串

因此,根据您的计算,如果要获取小数,请使用“ Float”,如果要获取数字,请使用“ int”作为强制转换。

结合以前的答案:

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

    which_area(int(raw_input()))

但是,连同此triangle_areacircle_areasquare_area均不返回任何内容-而是打印一条消息。 您可能想以以下形式结束这些功能:

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

(请注意,我在这里仅使用了简单的字符串连接-您可以改用旧样式或新样式的字符串格式)

函数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:”)之类的操作

  相关解决方案