2016-10-01 75 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": 
     # ... 

当然,所有的名字现在完全搞砸了,但是这是一个不同的问题...