2017-01-29 6 views
-2

私は学生のデータを記録して出力するサブルーチンやテキストファイルを使用しようとしていますが、私はオプション1,2および4のための無限ループを持っており、それは私のコードを 方法を修正する見当がつかない:サブルーチンに無限ループが続くのを誰か助けてくれますか?

def displayMenu(): 
    print("1. save to new file") 
    print("2. append to and existing file") 
    print("3. calculate the average mark") 
    print("4. display data") 
    choice = input("enter choice") 
    while int(choice) <1 or int(choice) >5: 
     choice = input("pick a valid option") 
    return choice 
def saveToFile(a): 
    studentMark = "0" 
    studentName = input("enter a student name, type xxx when you are done") 
    while studentName != "xxx": 
     file = open ("studentMark.txt", "w") 
     f = open("studentNames.txt", "w") 

     studentMark = input("Enter mark:") 
     f.write(studentName +"\n") 
     file.write(studentMark + "\n") 
     studentName = input("name") 
     f.close() 
     file.close() 

def appendToFile(b): 
    studentMark = "0" 
    studentName = input("enter a student name, type xxx when you are done") 
    while studentName != "xxx": 
     file = open ("studentMark.txt", "a") 
     f = open("studentNames.txt", "a") 

     studentMark = input("Enter mark:") 
     f.write(studentName +"\n") 
     file.write(studentMark +"\n") 
     studentName = input("name") 
     f.close() 
     file.close() 
def average(c): 
    total = 0.0 
    length = 0.0 
    average = 0.0 
    file2 = open("studentMark.txt", "r") 
    for line in file2: 

     amount = float(studentmark) 
     total += amount 
     length = length + 1 
    average = total/length 
    print("Average mark:", average) 
    file2.close() 
def printstuff(d): 
    o = open('output.txt', 'w') 

    fh = open("studentNames.txt", "r") 
    fh2 = open("studentMark.txt", "r") 

    for line in (fh.readlines()): 
     o.write(line.strip("\r\n") + "\t" + fh2.readline().strip("\r\n") + "\n") 
    o.close() 
    o = open("output.txt", "r") 
    output = o.read() 
    print(output) 

    fh.close() 
    fh2.close() 
    o.close() 

option = displayMenu() 
while option != "5": 
    if option == "1": 
     saveToFile("write") 
    elif option == "2": 
     appendToFile("append") 
    elif option == "3": 
     average("mark") 
    elif option == "4": 
     printstuff("display") 

print("quit") 

一部の旧式のコードとスタッフ

+1

'option'は変更されませんを変更することができないことによって、無限ループを作成してコピーし、フォーラムから編集されますループの中。なぜそれが決して終わらないのだろうか –

+0

それは問題だった – Billibob

答えて

0

があるかもしれないので、私のコードの平均値の算出部と、私は、ユーザーが自分のオプション

関連する問題