2017-03-11 9 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

あなたは輸入されたtrouble2あなたはそれを呼び出すように、ローカル名前空間にtask2から:

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()このエラーが発生しました:トレースバック(最新の最後の呼び出し): ファイル "C:\ Users \ Alex \ Downloads \ Task 3(1).py"、行37、 text_file.write(start + "\ n" ) TypeError:+: 'NoneType'と 'str'のためのサポートされていないオペランドタイプ – User592

関連する問題