【发布时间】: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()
我查看了与此类型错误有关的所有其他堆栈溢出问题,但没有一个实际上解决了相同的问题。
【问题讨论】:
-
top和middle是空字符串。您希望它如何进行任何格式化? -
@TigerhawkT3 看下面,他们是稍后分配的
-
不,他们不是。
selector()什么都不做。 -
它们都是被丢弃的局部变量。再说一次,如果你不相信我,你可以打印一下看看你有什么。
-
您应该阅读范围,尤其是函数和全局范围。
标签: python concatenation typeerror modulo