2017-03-11 194 views
0

作为我的计算课程的一部分,我需要创建一个程序来解决设备上的问题。对于任务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 
+0

的格式不堆栈溢出的是没有把它正确显示 – User592

+0

请阅读并遵守帮助文档中的发布准则。 [最小,完整,可验证的示例](http://stackoverflow.com/help/mcve) - 发布您的所有代码并不合适。 – AChampion

回答

0

你已经导入trouble2task2到局部名字空间,所以你只需要调用它:

from task2 import trouble2 
... 
trouble2() 

如果import task2那么你会称它为:

import task2 
... 
task2.trouble2() 
+0

谢谢,但我怎么会得到启动变量打印到相同的文本文件作为任务3的代码中的其他人? – User592

+0

避免使用全局变量,只需从'trouble2'中返回即可。 'start = trouble2()'。 – AChampion

+0

我试过:start = trouble2() text_file = open(str(caseNo)+“.txt”,“a”) text_file.write(start +“\ n”) text_file.close()提出了以下错误:Traceback(最近调用最后一次): 文件“C:\ Users \ Alex \ Downloads \ Task 3(1).py”,第37行,在 text_file.write(start +“\ n” ) TypeError:不支持的操作数类型为+:'NoneType'和'str' – User592

相关问题