简体   繁体   English

Python - 如何使我的其他程序仅在 if 语句一致时执行?

[英]Python - How do I make it so that my other program only executes when an if statement is in agreement?

My program is meant to make some sort of online cookbook and depending on what I want to make, I can choose which program to execute.我的程序旨在制作某种在线食谱,根据我想要制作的内容,我可以选择执行哪个程序。 In order to finally have got it to work, the integer I type in will correspond to the code that I want to run:为了最终让它工作,我输入的 integer 将对应于我要运行的代码:

enter code here

cookbook = int(input("What recipe would you like to make? (Please state the number)\n"))
if cookbook == 1:
    import mymodule as mx
    mx.greeting("Nana")

    a = mx.person1["age"]
    print(a)

    mx.omin(2)

    mx.tmin(4)

    mx.cmin(10)

    mx.smallest(0)
else:
    print("Updates pending")

It seems you want to change which python file you are going to run.您似乎想更改要运行的 python 文件。 You could do this simply by changing the namespace from the imports.您可以简单地通过更改导入的命名空间来做到这一点。

cookbook = input("What recipe would you like to make?")
if cookbook == "pepper":
    import pepper as recipe # This is the name of the file I want to execute
elif cookbook == "pasta":
    import pasta as recipe

recipe.run()

There can be more elegant ways to do it, and use the name itself to load the file directly, but I think this is closest to what you were trying to do.可以有更优雅的方法来做到这一点,并使用名称本身直接加载文件,但我认为这最接近你想要做的。

Here is an example with 3 files, as I understand what you are trying:这是一个包含 3 个文件的示例,因为我了解您正在尝试的内容:

cookbook.py食谱.py

cookbook = input("What recipe would you like to make? ")
if cookbook == "pepper":
    import pepper as recipe
elif cookbook == "pasta":
    import pasta as recipe

recipe.run()

pepper.py辣椒.py

def run():
    print('\n1: Grow plant')
    print('\n2: Get pepper')

pasta.py意大利面.py

def run():
    print('\n1: Mix water with flour and egg')
    print('\n2: Flatten')
    print('\n2: Boil in water')

You then run cookbook.py and type either 'pepper' or 'pasta'然后运行cookbook.py并输入“pepper”或“pasta”

What helps is that instead of using words to select which option is to be imported, use numbers and tell the user that in the question.有帮助的是,不要使用单词 select 要导入哪个选项,而是使用数字并在问题中告诉用户。 Functions don't seem to be able to process user input, so input it in the code before running as in "mx.omin(2)".函数似乎无法处理用户输入,因此在运行之前将其输入到代码中,就像在“mx.omin(2)”中一样。

cookbook = int(input("What recipe would you like to make? (Please state the number)\n"))
if cookbook == 1:
    import mymodule as mx
    mx.greeting("Nana")

    a = mx.person1["age"]
    print(a)

    mx.omin(2)

    mx.tmin(4)

    mx.cmin(10)

    mx.smallest(0)
elif cookbook == 2:
    import sauce as mx
    mx.greeting("Nana")

    mx.cabmin(0.6666)

    mx.carmin(2)

    mx.cookmin(200)

    mx.smallest(0)

    mx.confidence("OK")

    mx.final("sauce")
else:
    print("Updates pending")


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使我的程序读取多个 txt 文件并将其创建为 dataframe for python? - How do I make it so that my program reads multiple txt files and creates it into a dataframe for python? 如何制作 Python 程序,以便即使在终止后,变量、列表和其他对象的值也不会重置? - How can I make a Python program so that even after termination, the variable, list and other objects' values do not reset? Python / Pygame-如何制作程序,以便一旦按下某个键就可以激活其他任何程序? - Python/Pygame- How do I make a program so that once they press a key none other ones can be activated? 我将如何分辨Python程序何时出现错误,因此可以创建自己的自定义错误消息 - How would I make be able to tell when there is an error in my Python program, so I could create my own custom error message 如何制作只能采用整数的 if 语句? python 3 - How do I make an if statement that can only take integers ? python 3 Selenium Python - 如何让我的程序在某个特定元素出现时单击它? - Selenium Python - How do I make my program to click an particular element when it appeared? 我如何使我的python程序在精美的窗口中执行 - How do i make my python program execute in a fancy window 如何使我的python程序获取字符串的索引? - How do I make my python program get the index of a string? 如何从我的 Python 程序中制作 macOS 应用程序? - How do I make a macOS app out of my Python program? 我如何使我的程序进入python中的下一个elif - How do i make my program move on to the next elif in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM