+ 1

Goblin game / example from tutorial

Первая часть примера прошла хорошо, но после добавления второй часть примера вышло вот что: class GameObject: class_name = "" desc = "" objects = {} def __init__(self, name): self.name = name GameObject.objects[self.class_name] = self def get_desc(self): return self.class_name + "\n" + self.desc class Goblin(GameObject): def __init__(self, name): self.class_name = "goblin" self.desc = "A foul creature" super().__init__(name) def get_input(): command = input(":").split() verb_word = command[0] if verb_word in verb_dict: verb = verb_dict[verb_word] else: print("Unknown verb {}".format(verb_word)) return if len(command) >= 2: noun_word = command[1] print(verb(noun_word)) else: print(verb()) def say(noun): return 'You said "{}"'.format(noun) def examine(noun): if noun in GameObject.objects: return GameObject.objects[noun].get_desc() else: return "There is no {} here.".format(noun) goblin = Goblin("Gobbly") verb_dict = { "say":say, "examine":examine, } while True: get_input() Собственно ошибка, которая не дает мне перейти к 3 части примера: File "..\Playground\", line 41 else: ^ SyntaxError: invalid syntax

10th Dec 2016, 7:08 PM
Ulan Ortangalyiev
Ulan Ortangalyiev - avatar
2 Antworten
+ 3
i guess that else is after GameObject.objects[noun].get_desc() its because as soon as u broke out of if's indentation by using above line u ended if now this else is alone, it doesnt have previous if. to correct it u just indent GameObject.objects[noun].get_desc() -this line to previous if and code would be fine
10th Dec 2016, 7:18 PM
manish rawat
manish rawat - avatar
+ 1
Oh... That was simple. Thank you)
10th Dec 2016, 7:30 PM
Ulan Ortangalyiev
Ulan Ortangalyiev - avatar