我是一個初學者,最近開始python開發。 代碼我工作: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 , 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 reveal_occupants體。 「類型錯誤:類型‘NoneType’的對象沒有LEN()」
此錯誤如何可以克服的,並請建議的替代方法也
請發佈文本信息(代碼,錯誤)_文字_,而不是截圖。 – marcelm