【问题标题】:Why does this function not work when I put raw_input() as the argument?当我将 raw_input() 作为参数时,为什么这个函数不起作用?
【发布时间】:2014-04-19 13:49:58
【问题描述】:

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

【问题讨论】:

  • 选择 = raw_input; which_area(选择)
  • 你的区域函数没有返回结果

标签: python return nonetype


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    修改你的代码

    which_area(raw_input())
    

    which_area(int(raw_input()))
    

    【讨论】:

      【解决方案3】:

      根据您使用的 python 版本,可能有几个原因。

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

        which_area(int(input()))
        

      或者如果您使用的是 python 3.x 的波纹管版本,那么它应该是(ex - 2.7)

         which_area(int(raw_input()))
      

      无论哪种方式,请务必使用演员表,因为您正在使用输入来获取数字,但是

         raw_input() or input()
      

      函数将输入作为字符串

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

      【讨论】:

        【解决方案4】:

        结合之前的答案:

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

            which_area(int(raw_input()))
        

        但是,除了 thistriangle_areacircle_areasquare_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 :") 的操作

          【讨论】:

            猜你喜欢
            • 2022-01-04
            • 2021-10-04
            • 2021-06-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-07-25
            • 1970-01-01
            相关资源
            最近更新 更多