简体   繁体   中英

Why does this function not work when I put raw_input() as the argument?

So I have this short program that is supposed to give the area of a shape. When I give a value to which_area(x) when I call it, it works fine. But when I put raw_input() as the argument, (and because I've put print choose_area at the bottom), the shell replies None after input.

I'm not quite sure what's wrong but I think it has something to do with the return statement and how I've put it.

Any help is appreciated :) Please and thank you.

From a beginning programmer.

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 needs to return a value. Simple calculating the area isn't enough.

Change which_area(raw_input()) in the last line of choose_area to return which_area(raw_input()) .

change your code

which_area(raw_input())

to

which_area(int(raw_input()))

There could be few reasons depending on the python version you are using.

  1. Due to wrong python version if you are using the python version 3 or later version than that . it should be change to

     which_area(int(input())) 

or if you are using bellow version to python 3.x then it should be (ex - 2.7)

   which_area(int(raw_input()))

eitherway be sure to use the cast because you are using input to take a number but

   raw_input() or input()

functions take input as a String

So according to your calculations if you want to get fractional numbers use "Float" if you want to get who numbers use "int" as a cast .

To combine previous answers:

which_area requires an integer, raw_input() returns a string. You need:

    which_area(int(raw_input()))

But, along with this triangle_area , circle_area and square_area don't return anything - they print a message instead. You probably want to end these functions with something like:

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

(note that I have just used simple string concatenation here - you could use either old-style or new style string formatting instead)

the functions triangle_area, square_area and circle_area are not returning any values

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()

NOTE: raw_input takes promt as argument so you can do some thing like raw_input("Height :")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM