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

Python 将变量从一个模块打印到另一个模块?,python,python-3.x,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? ")

作为我计算机课程的一部分,我需要创建一个程序,为设备上的问题提供解决方案。对于任务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中不断遇到的错误:

回溯(最近一次呼叫最后一次):
文件“C:\Users\Alex\Downloads\Task 3(1).py”,第35行,在
任务2.麻烦2()
NameError:未定义名称“task2”

您已经将
trouble2
task2
导入到本地名称空间,因此您只需将其称为:

from task2 import trouble2
...
trouble2()
如果您
import task2
,则将其称为:

import task2
...
task2.trouble2()

格式设置不正确,因为堆栈溢出无法正确显示。请阅读并遵循帮助文档中的发布指南。-发布所有代码是不合适的。谢谢,但是我如何让开始变量打印到与任务3的代码中的其他变量相同的文本文件中?避免使用全局变量,只需从
trouble2
返回它,例如
start=trouble2()
。我已经尝试过:start=trouble2()text\u file=open(str(caseNo)+.txt),“a”)text\u file.write(start+“\n”)text\u file.close()我是如何遇到这个错误的:回溯(最近一次调用):文件“C:\Users\Alex\Downloads\Task 3(1).py”,第37行,在text\u file.write(start+“\n”)类型错误:不支持的操作数类型对于+:'NoneType'和'str',您可能需要阅读一些介绍性的python说明。您需要从
trouble2()
返回start。