python学習中の初心者です。
学習のため西暦を入力すると干支を返す会話botプログラムを作成しているのですが、作成したプログラムを実行すると、「NameError: name 'return_response' is not defined 」と表示され、解決ができません。いくら見比べてもどの部分が間違っているのか気づけず、分かる方がいらっしゃいましたら、ご教授お願い致します。
下記がコードになります。平成 1991 と入力すると未と返し、干支 1991と入力すると 1991年生まれの干支は未です。と返す、予定です。
関数及びモジュール化の勉強のため、プログラムが分かれています。
メインプログラム↓(bot_kumikomi11.py)
from pybot_eto1 import eto_command def len_command(command): cmd, text = command.split() length = len(text) response = '文字列の長さは {} です。'.format(length) return_response def heisei_command(command): heisei, year_str = command.split() year = int(year_str) if year >= 1989: heisei_year = year - 1988 response = '西暦{}年ハ、平成{}年デス!'.format(year, heisei_year) else: response = '西暦{}年ハ、平成デハアリマセン!'.format(year) return_response with open('pybot.text', encoding='utf-8') as command_file: raw_data = command_file.read() lines = raw_data.splitlines() bot_dict = {} for line in lines: word_list = line.split(',') key = word_list[0] response = word_list[1] bot_dict[key] = response while True: command = input('python> ') response = '' for key in bot_dict: if key in command: response = bot_dict[key] break if '平成' in command: response = heisei_command(command) if '長さ' in command: response = len_command(command) if '干支' in command: response = eto_command(command) if not response: response = '何を言ってるかワカラナイです!' print(response) if 'さようなら' in command: break
モジュールのプログラム↓(pybot_eto1.py)
def eto_command(command): eto, year = command.split() eto_list = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"] eto_number = (int(year) + 8) % 12 eto_name = eto_list[eto_number] response = '{}年生生まれの干支は「{}」です。'.format(year, eto_name) return_response 長くなりますが、よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー