从一个模块到另一个模块打印变量?

时间:2017-03-11 14:23:59

标签: python python-3.x

作为我的计算课程的一部分,我需要创建一个程序,为设备上的问题提供解决方案。对于任务3,我想使用我的任务2的修改版本作为模块的一部分而不是再次对其进行编码,我不断提出错误可以帮助这个代码吗?

任务3代码:

from random import randint
from task2 import trouble2
caseNo = randint(999,10000)

def main():

    devType = input("What type of device is it? ")
    devType = devType.lower()#converts the input devType to be lowercase

    text_file = open(str(caseNo)+".txt", "a")#opens/creates the file with the name that caseNo is and appends to it
    text_file.write(devType+"\n")#appends devtype to the file
    text_file.close()#closes the file

    devMake = input("What is the make of the device? ")
    devMake = devMake.lower()

    text_file = open(str(caseNo)+".txt", "a")
    text_file.write(devMake+"\n")
    text_file.close()

    devModel = input("What is the model of the device? ")

    text_file = open(str(caseNo)+".txt", "a")
    text_file.write(devModel+"\n")
    text_file.close()

    devMemory = input("How much memory does the phone have in GB? ")

    text_file = open(str(caseNo)+".txt", "a")
    text_file.write(devMemory+"\n")
    text_file.close()

main()
task2.trouble2()
global start
print(start)

任务2代码

from time import * #imports the time module
import os #imports the os module
import datetime #imports the date and time module


def trouble2():
    global start
    smashed = ['shattered', 'smashed', 'cracked'] #creates a list called smashed
    wifi = ['internet', 'conection', 'wifi'] #creates a list called wifi
    button = ['home', 'lock', 'button'] #creates a list called button
    temp = ['high', 'hot', 'cold', 'boiling', 'freezing', 'low', 'temp', 'temperature'] #creates a list called temp
    virus = ['virus', 'slow', 'bug'] #creates a list called virus
    wet = ['wet', 'water', 'damp', 'moist', 'soaking'] #creates a list called wet
    charge = ['low', 'battery', 'charge'] #creates a list called charge
    headphones = ['3.5mm', 'headphone', 'jack'] #creates a list called headphones
    bent = ['bent', 'warped', 'arched'] #creates a list called bent
    sound = ['volume', 'quiet', 'loud', 'silent', 'speaker', 'sound', 'sounds'] #creates a list called sound
    frozen = ['frozen', 'stuck', 'stopped']
    exits = ['bye' 'exit']
    date = [] #creates an empty list called date
    today = datetime.date.today()
    date.append(today) #adds the current date to the list date


##    print("Welcome to tech support the date today is",date[0]) #prints welcome line and the date
##    sleep(2) #pauses the program for 2 seconds
    start = input("What is the problem with your phone? ") #creates an input for the user to state the problem

    questionL = start.lower() # changes the name of a variable and makes it lower case

    phrase = questionL.split() # splits up the phrase to help in later code

    for words in phrase: #checks to see if phrase is empty
        if words in smashed: #checks to see if words contains anything from the list smashed
            #os.system('ohdear.jpeg') #opens a picture in the default program
            #sleep(10) #pauses the program for 10 seconds
            with open('task 2 solutions.txt') as f:
                l = []
                for lines in f:
                    l.append(lines)
                print(l[1])
                sleep(1)
                print(l[2])
                sleep(5)


        elif words in wet:
            with open('task 2 solutions.txt') as f:
                l = []
                for lines in f:
                    l.append(lines)
                print(l[17])
                sleep(1)
                print(l[18])
                sleep(1)
                print(l[19])
                sleep(5)
            pass
        elif words in wifi:
            with open('task 2 solutions.txt') as f:
                l = []
                os.system('tech.jpg')
                sleep(4)
                for lines in f:
                    l.append(lines)
                print(l[4])
                sleep(1)
                print(l[5])
                sleep(5)
            pass
        elif words in button:
            with open('task 2 solutions.txt') as f:
                l = []
                for lines in f:
                    l.append(lines)
                print(l[7])
                sleep(1)
                print(l[8])
                sleep(5)
            pass

        elif words in temp:
            with open('task 2 solutions.txt') as f:
                l = []
                os.system('hot.gif')
                sleep(7)
                for lines in f:
                    l.append(lines)
                print(l[10])
                sleep(1)
                print(l[11])
                sleep(1)
                print(l[12])
                sleep(5)
            pass


            sleep(5)
            pass

        elif words in virus:
            with open('task 2 solutions.txt') as f:
                l = []
                for lines in f:
                    l.append(lines)
                print(l[14])
                sleep(1)
                print(l[15])
                sleep(5)
            pass

        elif words in charge:
            with open('task 2 solutions.txt') as f:
                l = []
                for lines in f:
                    l.append(lines)
                print(l[21])
                sleep(1)
                print(l[22])
                sleep(1)
                print(l[23])
                sleep(5)
            pass

        elif words in headphones:
            with open('task 2 solutions.txt') as f:
                l = []
                for lines in f:
                    l.append(lines)
                print(l[25])

            pass

        elif words in bent:
            with open('task 2 solutions.txt') as f:
                l = []
                for lines in f:
                    l.append(lines)
                print(l[27])
                sleep(1)
                print(l[28])
                sleep(1)
                print(l[29])
                sleep(5)
            pass


        elif words in sound:
            with open('task 2 solutions.txt') as f:
                l = []
                for lines in f:
                    l.append(lines)
                print(l[31])
                sleep(1)
                print(l[32])
                sleep(5)
            pass

        elif words in frozen:
            with open('task 2 solutions.txt') as f:
                l = []
                for lines in f:
                    l.append(lines)
                print(l[34])
                sleep(1)
                print(l[35])
                sleep(5)
            pass
        elif words in exits:
            exit()


        else:
            print("That solution is not covered by the program sadly but we have noted this down for the technition to deal with.")

这是我在任务3中提出的错误:

Traceback (most recent call last):
  File "C:\Users\Alex\Downloads\Task 3 (1).py", line 35, in <module>
    task2.trouble2()
NameError: name 'task2' is not defined

1 个答案:

答案 0 :(得分:0)

您已将trouble2中的task2导入到本地命名空间中,因此您只需将其调用:

from task2 import trouble2
...
trouble2()

如果您import task2,那么您可以将其称为:

import task2
...
task2.trouble2()