我试图将值设置为'true',这样我就可以启动一个程序

时间:2016-04-16 14:40:31

标签: python function if-statement

  • 我是python的新手,请原谅基本错误。
  • 所以我创建了一个计算器,但是我想通过允许用户运行程序来改进它,如果他们输入'True',这会将'running'设置为true。反过来会运行计算器。然而,这似乎没有发生。

继承我的代码:

def start():
     print("Hello world!")
     name=input("Please enter your name: ")
     print("Hi {0}".format(name))
     run=input("type | True | to run the program: ").capitalize()
     if run== "True":
         running = True
     else:
         print("You need to enter | True | to run the program")

def main():
    if running== True:
        print("1 = Add")
        print("2 = Subtract")
        print("3 = Times")
        print("4 = Divide")
        print("5 = Quit program")
        calc=int(input("enter number of choise: "))   

1 个答案:

答案 0 :(得分:1)

您永远不会调用任何一项功能,您需要调用其中一项功能来启动您的程序,以获得所需的功能,您需要调用start()

然后,如果用户输入True

,只需调用你的main(),而不是使用那个怪异的running = True(无论如何都不会工作)。
def start():
     print("Hello world!")
     name=input("Please enter your name: ")
     print("Hi {0}".format(name))
     run=input("type | True | to run the program: ").capitalize()
     if run== "True":
         main() #the user typed true, so lets jump to your main function
     else:
         print("You need to enter | True | to run the program")

def main():
    print("1 = Add")
    print("2 = Subtract")
    print("3 = Times")
    print("4 = Divide")
    print("5 = Quit program")
    calc=int(input("enter number of choise: "))   


start() #the program never runs your start function without this line