Python 如何让该程序从用户';什么决定? 如果用户输入“是”,我希望程序重新开始。如果用户输入“否”,我希望它结束

Python 如何让该程序从用户';什么决定? 如果用户输入“是”,我希望程序重新开始。如果用户输入“否”,我希望它结束,python,Python,在代码中编辑: if answer == "yes" or answer=="Yes": return True elif answer == "no" or answer=="No": return False else: return import math def isValid(side1, side2, side3): if (((side1 + side2) > side3) and ((side1 + side3) > side2)

在代码中编辑:

if answer == "yes" or answer=="Yes":
    return True
elif answer == "no" or 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()
我的方式:

if answer == "yes" or answer=="Yes":
    return True
elif answer == "no" or 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()
减少打字

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

if answer == "yes" or answer=="Yes":
    return True
elif answer == "no" or 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()

你的代码不起作用。一旦你调用
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

我希望此代码能按要求工作。所作的修改如下:

  • 删除了将三角形长度作为输入时的.split()。u可以直接给出长度列表,而不是给出字符串[3,4,5]。它将自动分配side1=3、side2=4、side3=5。因此,所有侧面的侧1=评估(侧1)。input()以u-give的形式接受输入,与将输入转换为字符串的原始输入()不同
  • 在while循环中,如果答案为yes,则需要重复完整的代码
  • 答案=原始输入();将input()更改为raw_input(),正如我们期望的字符串一样。所以现在不需要在输入时用引号写“是”,只需写“是”而不用引号;它仍然将它作为字符串
  • 将答案转换为“lower”,意味着所有字母都将不大写,然后与“yes”或“no”进行比较
  • 用于控制while循环的标志,而不是返回。当您使用return时,函数终止并将值返回给like r。但是我们不想在这里结束函数,因此这里不应该使用return
  • 在else:block中插入返回x,以便在打印后,如果您想进一步使用该值,x也被指定给r。因此,将main()替换为r=main()


  • 我尝试使用您建议的编辑来运行程序,但仍然没有重新启动。您的程序中存在逻辑错误,等待将修复该错误并编写您正在使用的python版本。当我运行程序时,如果用户按“是”,它不会问用户是否要再次运行;下面是一个示例:不需要两个while循环,而break-inside-while循环只中断一个循环而不是整个程序当用户输入yes或no时使用内部while循环。在这个位置,我们必须提示他/她再次输入,但如果他想退出函数,他可以只输入“no”,然后main()将返回,不间断