首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python MadLib。NameError

Python MadLib。NameError
EN

Stack Overflow用户
提问于 2015-06-16 12:03:41
回答 2查看 835关注 0票数 0

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

"NameError:未定义名称'line‘“

这是我的代码:

代码语言:javascript
复制
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()

有人能帮我找出我做错了什么吗?谢谢

编辑

好的,我用line替换了line变量,现在当我运行它的时候

"TypeError:不能将'int‘对象隐式转换为str

因为数字不能用于字符串..。这次又是什么?

编辑2

代码语言:javascript
复制
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 = 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: "))
#Itterates through the file and replaces all words with user input
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)
#closes file
madLib1.close()

当我运行这个时,我没有错误,但是文件仍然和以前一样.

编辑3

对于每个给我提供如何打开和读取文件的建议的人来说,我现在有了这样的帮助:如何将用户提供的输入保存到文件中,以便以后可以读取它,或者创建一个新的文件。

下面是文本文件看起来像http://prntscr.com/7hlyag的样子

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-16 12:08:25

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

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

代码语言:javascript
复制
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

另一种选择是:

代码语言:javascript
复制
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]

票数 0
EN

Stack Overflow用户

发布于 2015-06-16 13:18:47

我建议您使用带语句打开一个文件。

代码语言:javascript
复制
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)

此外,您的函数接缝是不完整的,在替换文本之后,您可能希望将其保存在某个地方,但该部分在您的函数中完全丢失。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30867037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档