Pythonでは、私はRPGを作成しようとしています。私が "def townbar()"を実行するとき、定義されていないと言います。Python - 関数が定義されていません
このコードは、すべてが意味を作っていることを確認するために参照するためのものです:
class Player:
def __init__(self, name):
self.name = name
self.health2 = 100
self.health = self.health2
self.attack = 10
# Game
def main():
print("Welcome player.")
print("1. Start")
print("2. Load")
print("3. Exit")
option = input("> ")
if option == "1":
start()
elif option == "2":
pass
elif option == "3":
sys.exit()
else:
main()
def start():
print('\n' * 80)
print("Hello, what is your name?")
option = input("> ")
global PlayerIG
PlayerIG = Player(option)
start1()
def start1():
print('\n' * 80)
print("Name: %s" % PlayerIG.name)
print("Attack: %i" % PlayerIG.attack)
print("Health: %i/%i\n" % (PlayerIG.health, PlayerIG.health2))
print("1. Go to nearby town")
print("2. Stand here and do nothing")
option = input("> ")
if option == "1":
town()
if option == "2":
print("Really? This is the guy we chose to be the hero of this story?
*sigh* Pick again.")
start1()
else:
start1()
def town():
print('\n' * 80)
print("You arrive at the town and you see 3 signs.\n")
print("1. Go to the bar")
print("2. Go to the market")
print("3. Go to the king")
option = input("> ")
if option == "1":
townbar()
elif option == "2":
market()
elif option == "3":
print("The king ignores you, as you are but a peasant\n")
town()
else:
town()
は、これは私の問題を与える部分であり、それは(それがtownbarの古い名前だった)バーが定義されていないことを言います。私は名前を変更し、すべてが問題ないことを確認した後、それでもエラーが表示されます。
def townbar():
print('\n' * 80)
print("You see many people in the bar.")
print("1. Talk to the bartender")
print("2. Talk to the people in the bar")
print("3. Exit the bar")
option = input("> ")
if option == "1":
print("The bartender greets you.\n")
bartendertalk()
elif option == "2":
print("The people don't care enough or are too drunk to speak to
you.\n")
townbar()
elif option == "3":
town()
else:
townbar()
def bartendertalk():
print('\n' * 80)
print("Would you like to hear of the local news? [Y/N]")
option = input("> ")
if option == "Y":
print("Would you like to hear the gossip or some real news?\n")
print("1. Gossip")
print("2. Real news")
option2 = input("> ")
if option2 == "1":
print("I've heard that Ron has been cheating on Margaret with
Beatrice! Very sad.\n")
bartendertalk()
elif option2 == "2":
print("People talk of a destroyed castle holding centuries worth
of gold in there, but nobody has ever explored it.")
bartendertalk()
if option == "N":
print("Then why are you talking to me?\n")
townbar()
else:
bartendertalk()
main()
エラー:townbar()
はクラスの外の関数がある
File "C:\Users\Leo\Documents\Loadingscreens\PyCharm Community Edition
2017.3.2\helpers\pydev\pydevd.py", line 1668, in <module>
main()
File "C:\Users\Leo\Documents\Loadingscreens\PyCharm Community Edition
2017.3.2\helpers\pydev\pydevd.py", line 1662, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Users\Leo\Documents\Loadingscreens\PyCharm Community Edition
2017.3.2\helpers\pydev\pydevd.py", line 1072, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Users\Leo\Documents\Loadingscreens\PyCharm Community Edition
2017.3.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Leo/PycharmProjects/RPG Project/Game File.py", line 63, in
<module>
main()
File "C:/Users/Leo/PycharmProjects/RPG Project/Game File.py", line 21, in
main
start()
File "C:/Users/Leo/PycharmProjects/RPG Project/Game File.py", line 35, in
start
start1()
File "C:/Users/Leo/PycharmProjects/RPG Project/Game File.py", line 46, in
start1
town()
File "C:/Users/Leo/PycharmProjects/RPG Project/Game File.py", line 57, in
town
bar()
NameError: name 'bar' is not defined
たちにスタックトレースを表示します。そしてあなたのコードを適切にフォーマットしてください。 – ubadub
美しいコード - 今すぐ正しくフォーマットしてください。これを行う簡単な方法は、インデントをスペースに変更し、ここにコピーし、** {} **形式を使用することです。現在のところ、コードインデントが乱れており、Pythonにとって重要なことです。 –
@PatrickArtnerより簡単な方法があります。私は彼のために編集提案として提出しました。つまり、関数定義をインデントして中括弧を使ってすべてを取り消します。 – ubadub