はじめに
ChatGPT を使って言語間の変換を行わせるプロンプトです。
プロンプト
ここから実際のプロンプトです。
▼以下をコピペ
以下のコードをTypescriptで書き直して下さい。
var count = 0; timerID = setInterval('countup()',1000); function countup() { count++; console.log(count); }
▼回答例
以下が、TypeScriptに書き直したコードになります。
let count: number = 0; const timerID: number = setInterval(countup, 1000); function countup(): void { count++; console.log(count); }変更点の説明:
- 変数宣言に型を指定しました。count は数値型 (number)、timerID は数値型 (number) です。
- setInterval() の第一引数には、文字列形式で関数名を指定する代わりに、関数名そのものを直接渡すようにしました。
- countup() 関数の戻り値型に void を指定しました。
▼以下をコピペ
Pythonに書き換えて下さい
▼回答例
import time count = 0 # カウントの初期値 timerID = setInterval(countup, 1000) def countup(): global count count += 1 print(count)

コメントを残す