为什么会说“输入错误”?

时间:2014-04-06 20:16:16

标签: python-2.7

print "This is to find the area or perimeter of a rectangle or triangle?"
print "Do you want to find the area or perimeter of a rectangle(r) or triangle(t)?"
l= raw_input("I want to find the area or perimeter of a ")
if l=='r':
    print "Do you want to find the area(a) or perimeter(p) of your rectangle?"
    a= raw_input(" I want to find the ")
    if a=='p':
        print "What is the length of the rectangle?"
        b = int(raw_input("The length of the rectangle is "))
        print "What is the width of the rectangle?"
        c = int(raw_input("THe width of the rectangle is "))
        d = (2 * b) + (2 * c)
        print d
    elif a=='a':
        print "Got it. What is the length of your rectangle?"
        x = int(raw_input("The length of the rectangle is ")) 
        print "What is the width of your rectangle?"
        y = int(raw_input("The width of the rectangle is "))
        z = x * y
        print z
if l=='t':
    print "Do you want to find the area(a) or perimeter(p) of your triangle?"
    m= raw_input("I want to find the "
    if m == 'a':
        print "What is the length of your triangle?"
        n = int(raw_input("The length of the triangle is "
        print "What is the width of your triangle?
        o = int(raw_input("The width of the triagle is "
        q = (n * o)/2
        print q
    elif m=='p'
        print "What is the first length of your triangle?"
        r = int(raw_input("The first length of the triangle is ")
        print "What is the second length of the triangle?"
        s = int(raw_input("The second length of the triangle is ")
        print "What is the third length of the triangle?"
        u = int(raw_input(" The third length of the triangle is ")
        v = r + s + u
        print v

它表示该行的输入错误,说明m=='a'。请帮忙。

3 个答案:

答案 0 :(得分:2)

你有很多缺少方括号)。你必须检查它们是否匹配。每个(必须有相应的)

此外,在:if条件之后,您遗失了一些elif

<强>固定

print "This is to find the area or perimeter of a rectangle or triangle?"
print "Do you want to find the area or perimeter of a rectangle(r) or triangle(t)?"
l = raw_input("I want to find the area or perimeter of a ")
if l == 'r':
    print "Do you want to find the area(a) or perimeter(p) of your rectangle?"
    a = raw_input(" I want to find the ")
    if a == 'p':
        print "What is the length of the rectangle?"
        b = int(raw_input("The length of the rectangle is "))
        print "What is the width of the rectangle?"
        c = int(raw_input("THe width of the rectangle is "))
        d = (2 * b) + (2 * c)
        print d
    elif a == 'a':
        print "Got it. What is the length of your rectangle?"
        x = int(raw_input("The length of the rectangle is "))
        print "What is the width of your rectangle?"
        y = int(raw_input("The width of the rectangle is "))
        z = x * y
        print z
if l == 't':
    print "Do you want to find the area(a) or perimeter(p) of your triangle?"
    m = raw_input("I want to find the ")
    if m == 'a':
        print "What is the length of your triangle?"
        n = int(raw_input("The length of the triangle is "))
        print "What is the width of your triangle?"
        o = int(raw_input("The width of the triagle is "))
        q = (n * o) / 2
        print q
    elif m == 'p':
        print "What is the first length of your triangle?"
        r = int(raw_input("The first length of the triangle is "))
        print "What is the second length of the triangle?"
        s = int(raw_input("The second length of the triangle is "))
        print "What is the third length of the triangle?"
        u = int(raw_input(" The third length of the triangle is "))
        v = r + s + u
        print v

答案 1 :(得分:0)

更改行

m = raw_input("I want to find the "

m = raw_input("I want to find the ")

看来错误是由于没有关闭括号引起的。

答案 2 :(得分:0)

只是为了完整和完全过度杀伤:

from math import pi
import sys

# version compatibility shim
if sys.hexversion < 0x3000000:
    # Python 2.x
    inp = raw_input
else:
    # Python 3.x
    inp = input

#
# utility functions
#

def type_getter(datatype):
    def get_type(prompt):
        while True:
            try:
                return datatype(inp(prompt))
            except ValueError:
                pass
    return get_type

get_float = type_getter(float)
get_int   = type_getter(int)

def do_menu(prompt, choices):
    print("")
    # make the display value 1-based
    for i,choice in enumerate(choices, 1):
        print("{:>3}: {}".format(i, choice))
    # make the return value 0-based
    return get_int(prompt + "[1-{}] ".format(len(choices))) - 1

#
# base class
#

class Shape(object):
    name   = None   # subclass will override
    fields = []     #   these values

    def __init__(self, **kwargs):
        super(Shape, self).__init__()   # <= no longer needed in Python 3

        for field in self.fields:
            if field in kwargs:
                value = kwargs[field]
            else:
                value = get_float("What is the {}'s {}? ".format(self.name, field))
            setattr(self, field, value)

    def area(self):
        raise NotImplemented

    def perimeter(self):
        raise NotImplemented

#
# Subclasses
#        

class Triangle(Shape):
    name   = "triangle"
    fields = ["width", "height"]

    def area(self):
        return 0.5 * self.width * self.height

    def perimeter(self):
        # We will assume the width and height are
        #   adjacent and opposite sides of a right triangle
        adj, opp = self.width, self.height
        hyp = (adj*adj + opp*opp)**0.5
        return adj + opp + hyp

class Rectangle(Shape):
    name   = "rectangle"
    fields = ["width", "height"]

    def area(self):
        return self.width * self.height

    def perimeter(self):
        return 2 * (self.width + self.height)

class Ellipse(Shape):
    name   = "ellipse"
    fields = ["long_axis", "short_axis"]

    def area(self):
        return pi * self.long_axis * self.short_axis * 0.25

    def perimeter(self):
        # by Ramanujan's approximation - see
        # http://www.mathsisfun.com/geometry/ellipse-perimeter.html
        ra = 0.5 * self.long_axis
        rb = 0.5 * self.short_axis
        return pi * (3 * (ra + rb) - ((3 * ra + rb) * (ra + 3 * rb))**0.5)

known_shapes  = [Triangle, Rectangle, Ellipse]
known_actions = ["area", "perimeter"]

def main():
    print("Welcome to the shapifier!")

    # Which type of shape?
    shape_choice = do_menu(
        "What shape would you like? ",
        [shape.name for shape in known_shapes]
    )

    # Make a shape of that type!
    # (because of the way I wrote Shape.__init__,
    # it will automatically prompt for required values)
    shape = known_shapes[shape_choice]()

    # Which calculation?
    action_choice = do_menu(
        "What would you like to calculate? ",
        known_actions
    )

    # Perform the calculation
    value = getattr(shape, known_actions[action_choice])()

    # Display the result
    print("\nThe {} of the {} was {}.".format(known_actions[action_choice], shape.name, value))

if __name__=="__main__":
    main()