出现错误时如何使程序继续/重新启动?

时间:2017-04-21 23:12:42

标签: python python-2.7 python-3.x wxpython ipython

虽然我正在学习python我创建了这段代码,但问题是当有人输入与stm或mts不同的东西时,它只是打印错误消息并停止,我需要它继续并重新运行代码,所以我添加一个带循环语句的while循环,但它不能正常工作它只是不断发送错误消息,请任何想法如何让我的代码继续运行而不发送垃圾邮件或在它之后停止...并且非常感谢你!

以下是代码:

print("Welcome to MoroccanDirham_SaudiRiyal converter program!")

def mConv():
    def rial_saudi_to_moroccan_dirham(sar):
        amount = sar * 2.67
        print("Here is the result: ", amount, "MAD")

    def moroccan_dirham_to_rial_saudi(mad):
        amount = mad * 0.37
        print("Here is the result: ", amount, "SAR")
    usChoice = str(input("For SAR to MAD type stm and for MAD to SAR type mts: "))

    while True:
        if usChoice == str("stm"):
            x = int(input("Type the amount of money you want to convert: "))
            rial_saudi_to_moroccan_dirham(x)
            return False
        elif usChoice == str("mts"):
            y = int(input("Type the amount of money you want to convert: "))
            moroccan_dirham_to_rial_saudi(y)
            return False
        elif usChoice != str("stm") or usChoice != str("mts") :
            print("Error! Please choose between stm and mts.")
            continue
            return False
        else:
            return True
mConv()

1 个答案:

答案 0 :(得分:0)

移动usChoice = STR(输入("对于SAR到MAD类型STM和用于MAD到SAR类型MTS:&#34))  在while循环中删除你的最后一个else语句,你不应该在那里放一个返回,你可以写一条错误信息

打印("欢迎来到MoroccanDirham_SaudiRiyal转换程序!")

location /route {
    deny [my-ip];
}