Day 95簡単な設定ファイル読み込みツール

2026-05-30 JST ・ 難易度: 中級 ・ カテゴリ: 総合演習

Pythonコード

1import json2 3data = '''{4    "name": "John",5    "age": 30,6    "city": "New York"7}'''8 9# JSON文字列を辞書に変換10config = json.loads(data)11 12# 辞書の内容を表示13print(config)14 15# 名前と年齢を表示16print('名前:', config['name'])17print('年齢:', config['age'])18 19# ユーザーから好きな都市を入力してもらう20while True:21    try:22        favorite_city = input('好きな都市はどこですか?: ')23        break24    except Exception as e:25        print('入力エラー:', str(e))26 27# 好きな都市を表示28print('好きな都市:', favorite_city)

解説

次に試してみよう