初めましてpython初心者のgoliraです。
今、pythonの仮想環境vent上でrequestを下記プログラムで動かそうとしたのですが
プログラム自体は立ち上がるのですが、"天気"と打っても反応が無く、他のif文である
"干支"、"選ぶ"等のキーワードにも何故か反応しなくなりました。
どうすれば正常に動作するのか、ご教授お願いします。
1:プログラムコード
from import_eto import eto_command
from my_random import choice_command, dice_command
from import_hiduke import(kyou_command, ima_command,
youbi_command)
from forecast import weather_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()
_ if year_str.isdigit():
_ year = int(year_str)
_ if year >= 1989:
_ heisei_year = year - 1988
_ response = "西暦{}年は、平成{}年です".format(year, heisei_year)
_ else:
_ response = "西暦{}年は、平成ではありません".format(year)
_ else:
_ response = "数値を指定して下さい"
_ return response
aisatu_file = open("aisatu.txt", encoding="utf-8")
raw_data = aisatu_file.read()
aisatu_file.close
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("なんか言え ")
_ response = ""
_ try:
_ 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 "選ぶ" in command:
_ response = choice_command(command)
_ if "サイコロ" in command:
_ response = dice_command(command)
_ if "今日" in command:
_ response = kyou_command()
_ if "現在" in command:
_ response = ima_command()
_ if "曜日" in command:
_ response = youbi_command(command)
_ if "天気" in command:
_ response = weather_command()
_ if not response:
_ response = "日本語喋れや豚野郎"
_ print(response)
_ if "さようなら" in command:
_ print("二度と開くなよまじで")
_ break
except Exception as e: print("予期せぬエラーが発生しました") print("種類:", type(e)) print("内容:", e)
”import_eto.py”のコード
#coding:utf-8
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
"my_random.py"のコード
import random
def choice_command(command):
_ data = command.split()
_ choiced = random.choice(data[1:])
_ response = "「{}」が選ばれました".format(choiced)
_ return response
def dice_command(command):
_ num = random.randrange(1, 100)
_ response = "「{}」の目が出ました".format(num)
_ return response
"import_hiduke.py"のコード
rom datetime import date, datetime
def kyou_command():
_ today = date.today()
_ response = "今日の日付は{}です".format(today)
_ return response
def ima_command():
_ now = datetime.now()
_ response = "現在日時は{}です".format(now)
_ return response
def youbi_command(command):
_ try:
_ data = command.split()
_ year = int(data[1])
_ month = int(data[2])
_ day = int(data[3])
_ one_day = date(year, month, day)
_ weekday_str = "月火水木金土日"
_ weekday = weekday_str[one_day.weekday()]
_ response = "{}は、{}曜日です".format(one_day, weekday)
_ except IndexError:
_ response = "3つの値(年月日)を空白で区切って入力して下さい"
_ except ValueError:
_ response = "正しい日付を入力して下さい"
_ return response
"forecast.py"のコード
import requests
def weather_command():
_ base_url = "http://weather.livedoor.com/forecast/webservice/json/v1"
_ city_code = "230010"
_ url = "{}?city={}".format(base_url, city_code)
_ r = requests.get(url)
_ weather_data = r.json()
_ city = weather_data["location"]["city"]
_ label = weather_data["forecasts"][0]["dateLabel"]
_ telop = weather_data["forecasts"][0]["telop"]
_ response = "{}の{}の天気は「{}」です".format(city, label, telop)
_ return response
2:エラーコード
なんか言え 干支
予期せぬエラーが発生しました
種類: <class 'ValueError'>
内容: not enough values to unpack (expected 2, got 1)
なんか言え 選ぶ
予期せぬエラーが発生しました
種類: <class 'IndexError'>
内容: Cannot choose from an empty sequence
なんか言え 天気
なんか言え 天気
なんか言え 天気
回答1件
あなたの回答
tips
プレビュー