Python开发[类型错误]
我是一个初学者,最近开始了python开发。 我正在编写的代码:Python开发[类型错误],python,macvim,Python,Macvim,我是一个初学者,最近开始了python开发。 我正在编写的代码: import random import textwrap def show_message(dotted_line,width): print(dotted_line) print("\033[1m"+ "Attack of clones:" + "\033[0m") message = ( "The war between humans and their arch enemies ,
import random
import textwrap
def show_message(dotted_line,width):
print(dotted_line)
print("\033[1m"+ "Attack of clones:" + "\033[0m")
message = (
"The war between humans and their arch enemies , Clones was in the offing. Obi-Wan, one of the brave Jedi on his way ,"
"he spotted a small isolted settlement .Tired and hoping to replenish his food stock , he decided to take a detour."
"As he approached the village, he saw five residence , there was no one to be seen around.He decided to enter" )
print(textwrap.fill(message, width = width))
def show_mission(dotted_line):
print("\033[1m"+ "Mission:" + "\033[0m")
print('\t Choose the hit where Obi wan can rest...')
print("\033[1m"+ "TIP:" + "\033[0m")
print("Be careful as there are Stormtroopers lurking around!")
print(dotted_line)
def occupy_huts():
global huts
huts = []
while len(huts) < 5:
random_choice = random.choice(occupants)
huts.append(random_choice)
def process_user_choice():
message = "\033[1m"+ "Choose the hut to enter (1-5) " + "\033[0m"
uc = input("\n" + message)
index = int(uc)
print("Revealing the occupants...")
message = ""
def reveal_occcupants(index,huts,dotted_line):
for i in range (len(huts)):
occupant_info = "<%d:%s>"%(i+1,huts[i])
if i + 1 == index:
occipant_info = "\033[1m"+ "" + "\033[0m"
message += occupant_info + " "
print("\t" + message)
print(dotted_line)
def enter_huts(index,huts,dotted_line):
print("\033[1m"+ "Entering Hut %d ..." %index + "\033[0m")
if huts[index - 1] == 'clones':
print("\033[1m"+ "There's Stormtrooper Here!!" + "\033[0m")
else:
print("\033[1m"+ "It's Safe here!" + "\033[0m")
print(dotted_line)
def run():
keep_playing = 'y'
global occupants
occupants = ['clones','friend','Jedi Hideout']
width = 70
dotted_line = '-' * width
show_message(dotted_line, width)
show_mission(dotted_line)
while keep_playing == 'y':
huts = occupy_huts()
index = process_user_choice()
reveal_occcupants(index,huts,dotted_line)
enter_huts(index,huts,dotted_line)
keep_playing = raw_input("Play Again?(y/n)")
if __name__ == '__main__':
run()
而错误就在
def显示所有乘客。
TypeError:类型为“NoneType”的对象没有len
如何克服此错误,并请建议一种替代方法。len方法接受对象作为参数。 在您的例子中,在第43行,huts可能没有,因此您可能会得到一个错误 您应该在第42行之后插入如下条件
if huts is None:
return
我的猜测是,小屋不是一种类型,因为“占领小屋”从未被命名。或者huts变量的作用域有问题-可以通过在occude_huts函数外将其声明为空集来解决
此外,您还可以利用Python的语法,将第43行更改为for hut in huts:。如果您还需要小屋的索引,请尝试枚举小屋中的小屋、i-hut:。您的方法显示居住者作为小屋接收空值。也就是说,那种类型的小屋是没有的。这就是为什么无法获取此值的len。此处:
while keep_playing == 'y':
huts = occupy_huts()
您的Occuple_huts函数不返回任何内容—它填充了一个全局变量huts,但不返回它,因此如果您不显式返回某些内容,after the huts=occulation_huts语句huts现在是None—默认函数返回值。然后传递此now None huts变量以显示居住者:
解决方案很简单:修改Occups_huts,这样它就不会处理一个全局变量(这几乎总是一个非常糟糕的主意,并且不返回任何值),而是处理一个局部变量并返回它:
def occupy_huts():
huts = []
while len(huts) < 5:
random_choice = random.choice(occupants)
huts.append(random_choice)
return huts
然后在运行中:
有趣的是,你传递的参数通常是常量,对程序的逻辑没有影响,比如虚线,但是在重要的事情上使用全局变量——实际上应该是另一种方式,在模块开始时将虚线声明为伪常量,而不必麻烦将其传递给函数
另外,请注意,您在这里的“过程\用户\选择”也有类似的问题:
因为您的process\u user\u choice函数也不会返回任何内容。您应该对其进行修改,使其返回其局部变量索引。请将文本信息代码、错误作为文本而不是屏幕截图发布。调用Occuple_huts时,请检查run函数。这将掩盖症状,但无法解决真正的问题。
def occupy_huts():
huts = []
while len(huts) < 5:
random_choice = random.choice(occupants)
huts.append(random_choice)
return huts
def occupy_huts(occupants):
huts = []
while len(huts) < 5:
random_choice = random.choice(occupants)
huts.append(random_choice)
return huts
def run():
keep_playing = 'y'
occupants = ['clones','friend','Jedi Hideout']
# ...
while keep_playing == 'y':
huts = occupy_huts(occupants)
while keep_playing == 'y':
huts = occupy_huts()
index = process_user_choice()