试图调用读/写函数

时间:2015-02-21 03:21:54

标签: python function

我是一名初学程序员,在我的介绍游戏开发课程中学习python。当前的作业分配是创建一个读写文件的功能。我们还必须使用用户输入来查找文件目录。我目前遇到的问题是我无法弄清楚如何编写打开和关闭文件的功能。我会发布我的代码以获得任何帮助。谢谢。我暂时只专注于选项8。

enter = 0
start = 0
Roger = ()
text_file = ()
line = open("read.txt", "r")


def read_file(line):
    for line in text_file:
        print(line)
    return line

def close_file(text_file):
    text_file.close()
    return close_file

def update_score(scores, person, amount):
    for score in scores:
        if score[0].lower() == person.lower():
            score[1] += amount
    return scores

def update_score1(scores, person, amount):
    for score in scores:
        if score[0].lower() == person.lower():
            score[1] -= amount
    return scores

def addition(num1):
    return num1 + num1

def square(num):
    print("I'm in square")
    return num * num

def display(message):
    """Display game instuctions"""
    print(message)

def instructions():
    """Display game instuctions"""
    print("Welcome to the world's greatest game")


def main():
    str1 = ["Roger", 3456]
    str2 = ["Justin", 2320]
    str3 = ["Beth", 1422]
    instructions()
    scores = [str1, str2, str3]

    start = input("Would you like to view the high score options? y/n ")
    if start == "y":
        print("""\
        Hello! Welcome to the high scores!
        Here are the current high score leaders!:
        """)
        print(scores)
        print("""\n\
        0 - Sort high scores
        1 - Add high score
        2 - Reverse the order
        3 - Remove a score
        4 - Square a number
        5 - Add 2 numbers together
        6 - Add to a score
        7 - Subtract from a score
        8 - Read a file
        9 - Write to a file
        """)
        option = int(input("Please enter your selection "))
        while option < 8:
            print(scores)            
            if option == 0:
                scores.sort()
                print("These are the scores sorted alphabetically")
                print(scores)
                option = option = int(input("Please enter your selection"))
            elif option == 1:
                print(scores)
                print("Please enter your name and score; After entering your name, hit the return key and enter your score")
                name = input()
                score = int(input())
                entry = (name,score)
                scores.append(entry)
                print(scores)
                option = option = int(input("Please enter your selection"))
            elif option == 2:
                print(scores)
                scores.reverse()
                print("\nHere are the scores reversed")
                print(scores)
                option = option = int(input("Please enter your selection"))
            elif option == 3:
                print(scores)
                print("Please enter the high score you would like to remove. After typing the name, hit the return key and enter the score")
                name1 = input()
                score1 = int(input())
                remove = (name1,score1)
                scores.remove(remove)
                print(scores)
                option = option = int(input("Please enter your selection"))
            elif option == 4:
                val = int(input("Give me a number to square"))
                sqd = square(val)
                print(sqd)
                option = option = int(input("Please enter your selection"))
            elif option == 5:
                val0 = int(input("Give me one number"))
                val1 = int(input("Give me another number"))
                addi = (val0 + val1)
                print(addi)
                option = option = int(input("Please enter your selection"))
            elif option == 6:
                print(scores)
                sc0 = input("Please enter player whose score you would like to increase. ")
                sc1 = int(input("Please enter the amount would would like to add to their score. "))
                scores = update_score(scores, sc0, sc1)
                print(scores)                          
                option = option = int(input("Please enter your selection"))
            elif option == 7:
                sc2 = input("Please enter player whose score you would like to decrease. ")
                sc3 = int(input("Please enter the amount you would like to subtract from their score. "))
                scores = update_score1(scores, sc2, sc3)
                print(scores)
        while option <= 8:
            if option == 8:
                print (line)
                text_file.close()
                option = option = int(input("Please enter your selection"))


main()

1 个答案:

答案 0 :(得分:0)

返回文件内容的函数:

def read_file(filename):
  return open(filename).read()

要写入文件:

def write_file(filename, toWrite):
  file = open(filename, 'w')
  file.write(toWrite)
  file.close()

使用示例:

stuffInFile = read_file("myfile.txt")
write_file("myfile.txt", "I just wrote this to a file!")