Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当出现错误时,如何使程序继续/重新启动?_Python_Python 2.7_Python 3.x_Wxpython_Ipython - Fatal编程技术网

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

Python 当出现错误时,如何使程序继续/重新启动?,python,python-2.7,python-3.x,wxpython,ipython,Python,Python 2.7,Python 3.x,Wxpython,Ipython,在我学习python的过程中,我创建了这段代码,但问题是,当有人键入与stm或mts不同的内容时,它只打印错误消息并停止,我需要它继续并自行重新运行代码,因此我添加了一个带有continue语句的While循环,但它无法正常工作,它只是不断垃圾邮件发送错误消息,请告诉我如何让我的代码继续运行而不发送垃圾信息或在信息发送后停止..非常感谢 代码如下: print("Welcome to MoroccanDirham_SaudiRiyal converter program!") def mCon

在我学习python的过程中,我创建了这段代码,但问题是,当有人键入与stm或mts不同的内容时,它只打印错误消息并停止,我需要它继续并自行重新运行代码,因此我添加了一个带有continue语句的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()
Move usChoice=str(输入(“对于SAR到MAD类型stm,对于MAD到SAR类型mts:”) 在while循环中,删除最后一条else语句,您不应该在那里放一个return,您可以编写一条错误消息

打印(“欢迎来到摩洛哥汉迪拉姆·索迪里亚尔转换器项目!”)


非常感谢它的工作原理,我应该把usChoice=str(输入(“对于SAR-to-MAD-type stm,对于MAD-to-SAR-type mts:”)放在while循环中,正如你告诉我的那样,我真的没有注意它,但我知道我的代码应该在每次使用2时都重新获取另一个变量,你澄清了很多错误,先生我真的很感激!!
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()