signal = input(“Signal color?”)
if signal == “red”:
print(“Stop!”)
elif signal == “yellow”:
print(“Slow down!”)
elif signal == “blue” or signal == “green”:
print(“Go!”)
else:
print(“Invalid signal color…”)
signal = input(“Signal color?”)
if signal == “red”:
print(“Stop!”)
elif signal == “yellow”:
print(“Slow down!”)
elif signal == “blue” or signal == “green”:
print(“Go!”)
else:
print(“Invalid signal color…”)
eng_score = int(input(“English Score?”))
math_score = int(input(“Math Score?”))
if not (eng_score == 0 or math_score == 0):
print(“OK!”)
else:
print(“NG!”)
score = int(input(“Score? “))
if score > 80:
print(“OK!”)
print(“Good Job”)
else:
print(“NG!”)
print(“Nice try!”)
print(“End of program …”)
score = int(input(“Score? “))
if score > 80:
print(“OK!”)
print(“Good Job”)
print(“End of program …”)
initial_balance = int(input(“Initial Balance? “))
RATE = 1.1
print(f”Year 0: {initial_balance:,.2f}”)
print(f”Year 1: {initial_balance * RATE:,.2f}”)
print(f”Year 2: {initial_balance * RATE * RATE:,.2f}”)
class TodoList:
def init(self):
self.tasks = []
def add_task(self, task):
self.tasks.append(task)
def remove_task(self, task):
if task in self.tasks:
self.tasks.remove(task)
else:
print("タスクが見つかりません。")
def list_tasks(self):
if not self.tasks:
print("ToDoリストは空です。")
else:
print("ToDoリスト:")
for idx, task in enumerate(self.tasks, start=1):
print(f"{idx}. {task}")
def main():
todo_list = TodoList()
while True:
print(“\n操作を選択してください:”)
print(“1. タスクを追加”)
print(“2. タスクを削除”)
print(“3. タスク一覧を表示”)
print(“4. 終了”)
choice = input(“選択: “)
if choice == '1':
task = input("タスクを入力してください: ")
todo_list.add_task(task)
print("タスクが追加されました。")
elif choice == '2':
task_idx = int(input("削除するタスクの番号を入力してください: "))
if 1 <= task_idx <= len(todo_list.tasks):
task_to_remove = todo_list.tasks[task_idx - 1]
todo_list.remove_task(task_to_remove)
print("タスクが削除されました。")
else:
print("無効な番号です。")
elif choice == '3':
todo_list.list_tasks()
elif choice == '4':
print("プログラムを終了します。")
break
else:
print("無効な選択です。")
if name == “main“:
main()
fname = “Taro”
lname = “Yamada”
age = 32
separator = “=-“
print(separator * 10)
print(“I am {}{},{} years old!”.format(fname, lname,age))
print(f” {fname}{lname},{age} years old!”)
print(separator * 10)
import requests
from bs4 import BeautifulSoup
def crawl(url, max_depth=2):
if max_depth < 0:
return
try:
response = requests.get(url)
content = response.content
soup = BeautifulSoup(content, ‘html.parser’)
links = set()
for link in soup.find_all(‘a’):
href = link.get(‘href’)
if href and href.startswith(‘http’):
links.add(href)
print(f”Found {len(links)} links at {url}”)
for link in links:
crawl(link, max_depth – 1)
except requests.RequestException as e:
print(f”Error during requests to {url} : {str(e)}”)
# 使用例
start_url = “https://b.hatena.ne.jp/” # スタートするURL
crawl(start_url, max_depth=2) # 深さ2でクロール
mari.py
from flask import Flask, render_template, request
import random
import markovify
from transformers import GPT2LMHeadModel, GPT2Tokenizer
app = Flask(name)
tokenizer = GPT2Tokenizer.from_pretrained(“gpt2”)
model = GPT2LMHeadModel.from_pretrained(“gpt2”)
text_data = []
def generate_gpt2_text(prompt, max_length=100):
input_ids = tokenizer.encode(prompt, return_tensors=”pt”)
output = model.generate(input_ids, max_length=max_length, num_return_sequences=1)
response = tokenizer.decode(output[0], skip_special_tokens=True)
return response
def build_markov_model(data):
text_model = markovify.NewlineText(data)
return text_model
def generate_text_with_markov(model, num_sentences=3):
generated_text = model.make_sentence()
return generated_text
@app.route(“/”, methods=[“GET”, “POST”])
def chatbot():
user_input = “”
gpt2_response = “”
markov_response = “”
if request.method == "POST":
user_input = request.form["user_input"]
# ユーザーからの入力をGPT-2に送信し、生成されたテキストを取得
gpt2_response = generate_gpt2_text(user_input)
# GPT-2の生成テキストをリストに追加
text_data.append(gpt2_response)
# マルコフ連鎖モデルを使って新しいテキストを生成
markov_response = generate_text_with_markov(build_markov_model(text_data))
return render_template("chat.html", user_input=user_input, gpt2_response=gpt2_response, markov_response=markov_response)
if name == “main“:
app.run(debug=True)
templates
chat.html
Chatbot
ユーザー: 送信
{{ user_input }}
{{ gpt2_response }}
{{ markov_response }}