Python MadLib。 NameError

时间:2015-06-16 12:03:41

标签: python

我是一名初学python程序员,为课程编写madlib程序。我要求程序从文件中读取,然后在“[]”中找到需要替换的单词。我要求用户向我提供单词,然后尝试替换它们。但是,当我尝试line.replace(“[名词]”,名词)时,它给了我一个错误:

  

“NameError:名称'line'未定义”

这是我的代码:

MKMapRect mapRect = mapView.visibleMapRect;
for (NSDictionary *item in array)
{
    CLLocation *location = item[@"location"]; // Or what ever you store...
    MKMapPoint mapPoint = 
    MKMapPointForCoordinate(location.coordinate);
    BOOL inside = MKMapRectContainsPoint(mapRect, mapPoint);
}

有人可以帮我弄清楚我在这里做错了什么吗? 谢谢

修改

好的,我将def madLibOne(): #Function opens up a file that contains the madlib. madLib1 = open('MadLibOne.txt', 'r') madLib1.readline() #Reads the file and asks user for input in what type of words they want inputted. weekday = input("Enter a weekday: ") adjective = input("Enter an adjective: ") food = input("Enter a food: ") location = input("Enter a location: ") favoriteprof = input("Enter the name of your favorite Proffessor: ") sumOfMoney = eval(input("Enter a sum of money: ")) #Itterates through the file and replaces all words with user input for i in madLib1: line.replace("[weekday]",weekday) line.replace("[adjective]",adjective) line.replace("[food]",food) line.replace("[location]",location) line.replace("[favoriteProf]",favoriteprof) line.replace("[sumOfMoney]",sumOfMoney) #closes file madLib1.close() 变量替换为i,现在当我运行它时,我得到了

  

“TypeError:无法将'int'对象隐式转换为str”

因为数字不能用于字符串......现在是什么?

编辑2

line

当我运行它时,我没有错误,但文件仍然保持不变...

编辑3

对于向我提供有关如何打开和阅读文件的建议的每个人,我现在有了更好的帮助,如何将用户提供的输入保存到文件中,以便以后可以阅读或创建一个新文件。

以下是文本文件的内容http://prntscr.com/7hlyag

2 个答案:

答案 0 :(得分:0)

迭代变量名为i,而不是line

似乎您要从文件中读取并写入文件。这是最简单的方法:

import fileinput
weekday = input("Enter a weekday: ")
adjective = input("Enter an adjective: ")
food = input("Enter a food: ")
location = str(input("Enter a location: "))
favoriteprof = input("Enter the name of your favorite Professor: ")
sumOfMoney = str(input("Enter a sum of money: "))
for line in fileinput.FileInput("MadLibOne.txt",inplace=1):
    line = line.replace("[weekday]",weekday)
    line = line.replace("[adjective]",adjective)
    line = line.replace("[food]",food)
    line = line.replace("[location]",location)
    line = line.replace("[favoriteProf]",favoriteprof)
    line = line.replace("[sumOfMoney]",sumOfMoney)
    print line

可替换地:

weekday = input("Enter a weekday: ")
adjective = input("Enter an adjective: ")
food = str(input("Enter a food: "))
location = str(input("Enter a location: "))
favoriteprof = str(input("Enter the name of your favorite Proffessor: "))
sumOfMoney = str(eval(input("Enter a sum of money: ")))

#Values are:
# weekday = "Sunday"
# adjective = "Fun"
# food = "Beef"
# location = "Here"
# favoriteprof = "Kurland"
# sumOfMoney = "217"

with open('MadLibOne.txt', 'r') as madLib1:
    with open('MadLibOne_new.txt', 'w') as madLib1_new:
        for line in madLib1:
            line = line.replace("[weekday]",weekday)
            line = line.replace("[adjective]",adjective)
            line = line.replace("[food]",food)
            line = line.replace("[location]",location)
            line = line.replace("[favoriteprof]",favoriteprof)
            line = line.replace("[sumOfMoney]",sumOfMoney)
            madLib1_new.writelines(line)

文本文件包含:

  

“你好吗?”   今天是星期日!很好玩。   我喜欢吃牛肉,而我[动词]。   我应该上课,但我在这里。   我的教授是库兰德。他欠我217。

不要忘记纠正教授的拼写错误,并替换[verb]

答案 1 :(得分:0)

我建议您使用with statement打开文件。

from __future__ import with_statement #if you are using python2

def madLibOne():
    words = ["[weekday]","[adjective]","[food]","[location]","[favoriteProf]","[sumOfMoney]"]
    weekday = str(input("Enter a weekday: "))
    adjective = str(input("Enter an adjective: "))
    food = str(input("Enter a food: "))
    location = str(input("Enter a location: "))
    favoriteprof = str(input("Enter the name of your favorite Proffessor: "))
    sumOfMoney = str(input("Enter a sum of money: "))
    #Open the file
    with open('MadLibOne.txt', 'r') as f:
        with open('output.txt', 'w') as o:
            #Iterate over each line
            for line in f:
                newline = line
                newline = newline.replace("[weekday]",weekday)
                newline = newline.replace("[adjective]",adjective)
                newline = newline.replace("[food]",food)
                newline = newline.replace("[location]",location)
                newline = newline.replace("[favoriteProf]",favoriteprof)
                newline = newline.replace("[sumOfMoney]",sumOfMoney)
                o.write(newline)

此外,您的功能接缝不完整,更换文本后您可能希望将其保存在某个地方但该部分在您的功能中完全缺失。