2016-10-01 87 views
0

你好,我一直在處理這個簡單的腳本,我遇到了一些相當惱人的問題,我無法使用def來修復自己。和導入功能,它只是不會工作。這裏是主要的腳本導入腳本的問題

import time   # This part import the time module 
import script2  # This part imports the second script 

def main(): 
    print("This program is a calaulater have fun using it") 
    name = input("What is your name? ") 
    print("Hello",name) 
    q1 = input("Would you like to some maths today? ") 

if q1 == "yes": 
    script2 test() 

if q1 == "no": 
    print("That is fine",name,"Hope to see you soon bye") 
    time.sleep(2) 



if __name__ == '__main__': 
    try: 
     main() 
    except Exception as e: 
     time.sleep(10)  

在這裏,然後第二個劇本叫SCRIPT2的是,腳本以及 進口時間

def test(): 
    print("You would like to do some maths i hear.") 
    print("you have some truely wonderfull option please chooice form the list below.") 

這是我的腳本,目前卻DEOS不工作,請幫助我。

+2

您遇到的錯誤是什麼? – Efferalgan

+0

嘗試'script2.test()'而不是'script2 test()' – Jason

回答

0

首先你的縮進似乎並不正確。正如zvone所言。其次,你應該使用script2.test()而不是script2 test()。功能代碼是

import time   # This part import the time module 
import script2  # This part imports the second script 

def main(): 
    print("This program is a calaulater have fun using it") 
    name = input("What is your name? ") 
    print("Hello",name) 
    q1 = input("Would you like to some maths today? ") 

    if q1 == "yes": 
     script2.test() 

    if q1 == "no": 
     print("That is fine",name,"Hope to see you soon bye") 
     time.sleep(2) 



if __name__ == '__main__': 
    try: 
     main() 
    except Exception as e: 
     time.sleep(10) 
0

這是一個錯誤:

def main(): 
    #... 
    q1 = input("Would you like to some maths today? ") 

if q1 == "yes": 
    # ... 

首先,在main()q1q1外面是不一樣的變量。

其次,if q1 == "yes":q1 = input(...)之前執行,因爲main()尚未被調用。

的解決辦法是從返回主的q1值,然後才使用它:

def main(): 
    # ... 
    return q1 

if __name__ == '__main__': 
    # ... 
    result_from_main = main()  
    if result_from_main == "yes": 
     # ... 

當然,所有的名字現在完全搞砸了,但是這是一個不同的問題...