【问题标题】:Modulo TypeError: not all arguments converted during string formattingModulo TypeError:并非所有参数都在字符串格式化期间转换
【发布时间】:2015-12-20 01:45:18
【问题描述】:

当尝试使用模数插入文本时,它给了我

TypeError:字符串格式化期间并非所有参数都转换

在story()函数中

from random import *
f = open('madlib.txt', 'a')
head = ["You are a %s %s. You %s out of your %s to the sound of %s %s.", "You see a %s %s down the street. You %s to go catch it, but you trip on a %s. You hear %s %s is your ears."]
body = ["You make your way down the %s, and %s. You %s head first on to the %s. You %s and get back up.", "Unlike usual, you open your %s and get to work. You dont like to %s, so you %s. Your %s has no power over you! You reluctantly %s the urges and open it back up..."]
end = ["You get on your %s and %s. You need to %s your %s %s. You press the submit %s, and hope it gets in on time!", "You open your %s and %s. You casually %s for %s. You wonder why this %s is so random! You close your %s and go to sleep."]
top = ""
middle = "" 
bottom = ""
top_list = []
middle_list = []
end_list = []
def selector():
    num = randint(1,2)
    if num == 1:
        top = head[1]
    elif num == 2:
        top = head[0]
    num = randint(1,2)
    if num == 1:
        middle = body[1]
    elif num == 2:
        middle = body[0]
    num = randint(1,2)
    if num == 1:
        bottom = end[1]
    elif num == 2:
        bottom = end[0]
def runner():
    top_list.append(str(input("adjective")))
    top_list.append(str(input("noun")))
    top_list.append(str(input("verb")))
    top_list.append(str(input("noun")))
    top_list.append(str(input("verb -ing")))
    top_list.append(str(input("plural noun")))
    top_list.append(str(input("plural noun")))
    middle_list.append(str(input("noun")))
    middle_list.append(str(input("verb")))
    middle_list.append(str(input("verb")))
    middle_list.append(str(input("noun")))
    middle_list.append(str(input("verb")))
    end_list.append(str(input("noun")))
    end_list.append(str(input("verb")))
    end_list.append(str(input("verb")))
    end_list.append(str(input("school subject")))
    end_list.append(str(input("noun")))
    end_list.append(str(input("noun")))
def story():
    tuple(top_list)
    tuple(middle_list)
    tuple(end_list)
    f.write(top % tuple(top_list))
    print(top % tuple(top_list))
    f.write(middle % tuple(middle_list))
    print(middle % tuple(middle_list))
    f.write(end % tuple(end_list))
    print(end % tuple(end_list))
selector()
runner()
story()

我查看了与此类型错误有关的所有其他堆栈溢出问题,但没有一个实际上解决了相同的问题。

【问题讨论】:

  • topmiddle 是空字符串。您希望它如何进行任何格式化?
  • @TigerhawkT3 看下面,他们是稍后分配的
  • 不,他们不是。 selector() 什么都不做。
  • 它们都是被丢弃的局部变量。再说一次,如果你不相信我,你可以打印一下看看你有什么。
  • 您应该阅读范围,尤其是函数和全局范围。

标签: python concatenation typeerror modulo


【解决方案1】:

您的代码有 3 个问题,第一个是函数 selector 在此代码中,您没有像您想象的那样保存外部变量中的更改,而是保存在函数的内部,因为当你做top=head[0] python 所做的是在函数的命名空间中创建一个名为 top 的新变量,而不要管外部变量,要解决这个问题,你有几个选项,比如返回这些值然后分配给相应的变量或告诉使用global 命令来使用外部函数,如下所示:

def selector():
    global top,middle,bottom
    top    = head[ randint(0,1) ]
    middle = body[ randint(0,1) ]
    bottom = end[ randint(0,1) ]

也不需要所有这些 if,因为 randind 可以直接为您提供如上所示的索引。

第二个问题在函数runner 中,在这里你用 7 个项目而不是 6 个项目填充你的 top_list,因为你用 6 %s 定义了你的顶部文本

最后在story 中,您使用的是end 列表而不是bottom 字符串,而且您在这里做了多余的事情,该函数的前3 行什么也没做,因为您不保存结果,所有其他对 tuple 的调用都一遍又一遍地做同样的工作,以消除冗余这样做

def story():
    top_text    = tuple(top_list)
    middle_text = tuple(middle_list)
    end_text    = tuple(end_list)
    f.write(top % top_text)
    print(top % top_text)
    f.write(middle % middle_text)
    print(middle % middle_text)
    f.write(bottom % end_text)  #is bottom, no end
    print(bottom % end_text)    #is bottom, no end

【讨论】:

    【解决方案2】:

    您提供的是列表,而不是元组,请尝试: f.write(top % tuple(top_list))

    【讨论】:

    • 我在函数的开头做了元组
    • 当你调用元组时你没有分配它们,如果你想这样做top_list = tuple(top_list)
    • 我按照你在我的程序中说的做了,它调用了同样的错误
    • 这最终会是一个问题,但当前的问题是由不同的错误消息引起的另一个问题。
    猜你喜欢
    • 2020-08-23
    • 2015-10-28
    • 2015-01-21
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多