2016-04-16 50 views
0
  • 我是新來的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

你永遠不調用兩個函數,你需要調用這些函數來啓動你的程序,你需要調用start()

您所需的功能

然後,而不是使用怪異運行= TRUE(這不會反正工作),僅僅只有打電話給你的main(),如果用戶進入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 
+0

是的,這工作完美,謝謝。 –

相關問題