如何让这个程序从用户的决定开始?

时间:2015-06-25 05:12:04

标签: python

如果用户输入“是”,我希望程序重新开始。如果用户输入'no',我希望它结束​​

def isValid(side1, side2, side3):
    if (((side1 + side2) > side3) 
        and ((side1 + side3) > side2) 
        and ((side2 + side3) > side1)):
            return True
    else:
        return False

def area(side1, side2, side3):
    s = (side1 + side2 + side3) / 2
    area = round(math.sqrt(s * (s - side1) * (s - side3) * (s - side2)), 2)
    return area;

def main():
    side1, side2, side3 = input("Give me the lengths of a triangle's 3 sides?: ").split()
    side1 = eval(side1)
    side2 = eval(side2)
    side3 = eval(side3)

    valid = True

    valid = isValid(side1, side2, side3)

    while True:
        if valid is not True:
            print("This is not a valid triangle.")
            print()
            print("Would you like to go again? ")
            answer = input()
        if answer == "yes" or "Yes":
            return True
        elif answer == "no" or "No":
            return False
        else:
            return
        break
    else:
        x = area(side1, side2, side3)
        print("The area of the triangle is ", x)

main()

4 个答案:

答案 0 :(得分:1)

def main():
    while True:
        side1, side2, side3 = input("Give me the lengths of a triangle's 3 sides?: ").split()
        side1 = eval(side1)
        side2 = eval(side2)
        side3 = eval(side3)

        valid = isValid(side1, side2, side3)
        if not valid:
            print("This is not a valid triangle.")
            print()
            while True:
                print("Would you like to go again? (yes/no)")
                answer = input()
                if answer in ("no", "No"):
                    return
                elif answer in ("yes", "Yes"):
                    break
        else:
            x = area(side1, side2, side3)
            print("The area of the triangle is ", x)
            return

答案 1 :(得分:0)

在您的代码中进行修改:

if answer == "yes" or answer=="Yes":
    return True
elif answer == "no" or answer=="No":
    return False
else:
    return

我的方式:

减少输入

answer = input()
answer=answer.lower()
if answer == "yes" :
     return True
elif answer == "no":
     return False
else:
     return

<强>修饰:

import math
def isValid(side1, side2, side3):
    if (((side1 + side2) > side3)  and ((side1 + side3) > side2) and ((side2 + side3) > side1)):
            return True
    else:
        return False

def area(side1, side2, side3):
    s = (side1 + side2 + side3) / 2
    area = round(math.sqrt(s * (s - side1) * (s - side3) * (s - side2)), 2)
    return area;

def main():
    while True:
        side1, side2, side3 = input("Give me the lengths of a triangle's 3 sides?: ").split()
        side1 = int(side1)
        side2 = int(side2)
        side3 = int(side3)

        valid = True

        valid = isValid(side1, side2, side3)


        if valid is not True:
            print("This is not a valid triangle.")
            print("\n")
            print("Would you like to go again? ")
            answer = input()
            answer=answer.lower()
            if answer == "yes" :
                continue
            elif answer == "no":
                break
            else:
                break
        else:
            x = area(side1, side2, side3)
            print("The area of the triangle is ", x)

main()

答案 2 :(得分:0)

您的代码无效。一旦你打电话给return True,你的程序就会结束,如果用户说'是',你看起来好像要继续循环。

def area():
    side1, side2, side3 = input("Give me the lengths of a triangle's 3 sides?: ").split() 
    side1 = eval(side1) 
    side2 = eval(side2) 
    side3 = eval(side3)
    valid = isValid(side1, side2, side3)
    if valid:
        x = area(side1, side2, side3)
        print("The area of the triangle is ", x)
        return True
    else:
        print("This is not a valid triangle.\n")
        print("Would you like to go again? ")
        answer = input().lower()
        if answer == 'no':
            return False
        else:
            return area()

我们可以使迭代而不是递归,如果我们期望一个邪恶的克星通过每次给你的脚本一个无效的三角形而导致堆栈溢出。

def area():
    side1, side2, side3 = input("Give me the lengths of a triangle's 3 sides?: ").split() 
    side1 = eval(side1) 
    side2 = eval(side2) 
    side3 = eval(side3)
    valid = isValid(side1, side2, side3)
    if valid:
        x = area(side1, side2, side3)
        print("The area of the triangle is ", x)
        return True
    else:
        return False

while True:
    success = area()
    if success:
        return True
    else:
        print("This is not a valid triangle.\n")
        print("Would you like to go again? ")
        answer = input().lower()
        if answer == 'no':
            return False

答案 3 :(得分:0)

我希望这段代码可以按要求运行。所做的更改是:

  1. 以三角形长度作为输入时删除了.split()。而不是给出字符串,你可以直接给出长度列表为[3,4,5]。它将自动分配side1 = 3,side2 = 4,side3 = 5。因此删除了所有边的side1 = eval(side1)。 input()以你给它的形式接受输入,不像raw_input()将输入转换为字符串。
  2. 带来完整的代码,如果在循环中回答是肯定的话需要重复。
  3. answer = raw_input();将input()更改为raw_input(),因为我们在这里期望字符串。所以现在不需要在输入处用引号写'是',只需在没有引号的情况下写“是”;它仍然会把它当作字符串。
  4. 将答案转换为'lower',表示所有字母都不会被大写,然后与'yes'或'no'进行比较
  5. 使用标志来控制while循环,而不是返回。当你使用return时,函数终止并在这种情况下将值返回到r。但是我们不想在那里结束我们的功能,因此不应该在这里使用返回。
  6. 在else:block中插入return x,这样在打印之后,如果你想进一步使用该值,x也被分配给r。因此用r = main()

    替换了main()
    def isValid(side1, side2, side3):
        if (((side1 + side2) > side3) and ((side1 + side3) > side2) and ((side2 + side3) > side1)):
            return True
        else:
            return False
    
    def area(side1, side2, side3):
        s = (side1 + side2 + side3) / 2
        area = round(math.sqrt(s * (s - side1) * (s - side3) * (s - side2)), 2)
        return area
    
    def main():
        flag = True
        while flag:
            side1, side2, side3 = input("Give me the lengths of a triangle's 3 sides?: ")
    
            valid = isValid(side1, side2, side3)
            if valid is not True:
                print("This is not a valid triangle.")
                print()
                print("Would you like to go again? ")
                answer = raw_input()
                if answer.lower() == "yes":
                    flag = True
                elif answer.lower() == "no":
                    flag = False
    
            else:
                x = area(side1, side2, side3)
                print("The area of the triangle is ", x)
                return x
    
        r = main()