出现Error时如何让程序continue/restart?

How to make the program continue/restart when there is an Error?

虽然我正在学习 python 我创建了这段代码,但问题是当有人键入不同于 stm 或 mts 的内容时,它只会打印错误消息并停止,我需要它继续并重新 运行 代码本身,所以我添加了一个带有 continue 语句的 while 循环,但它不能正常工作,它只是不断发送错误消息,请知道如何让我的代码保持 运行ning 而不发送垃圾消息留言或停止..非常感谢!

代码如下:

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

移动我们Choice = str(input("For SAR to MAD type stm and for MAD to SAR type mts: ")) 在 while 循环中并删除最后一个 else 语句,你不应该在那里放一个 return,你可以写一条错误消息

打印("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")


    while True:
        usChoice = str(input("For SAR to MAD type stm and for MAD to SAR type mts: "))
        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
        else:
            print("Invalid choice. Allowed choices");
            print("stm - rial to dirham");
            print("mts - dirham to rial");
mConv()