Skip to content

Commit 2655aee

Browse files
author
Marty
committed
test test.html
1 parent 113761b commit 2655aee

File tree

1 file changed

+34
-45
lines changed

1 file changed

+34
-45
lines changed

test.html

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
}
1919
#output {
2020
width: 100%;
21-
height: 200px;
21+
height: 400px;
2222
background-color: #f5f5f5;
2323
padding: 10px;
2424
box-sizing: border-box;
@@ -36,38 +36,9 @@
3636
</head>
3737
<body>
3838
<h1>Pyodide Python 執行器</h1>
39-
<textarea id="python-code" placeholder="在此輸入您的 Python 程式碼,例如:&#10;print('Hello, Pyodide!')">
40-
import random
41-
def guess_number_game():
42-
while True:
43-
# 顯示遊戲開始的提示
44-
print("遊戲開始,請從1到50中,隨意輸入一個數字。")
45-
46-
# 隨機生成1到50的數字
47-
correct_number = random.randint(1, 50)
48-
49-
while True:
50-
# 玩家輸入數字
51-
player_guess = int(input("請輸入你的猜測: "))
52-
53-
# 比較玩家的猜測與正確答案
54-
if player_guess > correct_number:
55-
print("再小一點")
56-
elif player_guess < correct_number:
57-
print("再大一點")
58-
else:
59-
print("恭喜你答對")
60-
break # 猜對了,跳出內部迴圈
61-
62-
# 詢問玩家是否要繼續遊戲
63-
continue_game = input("是否要繼續遊戲? (是/否): ")
64-
if continue_game.lower() != '是':
65-
print("感謝遊玩,遊戲結束!")
66-
break # 結束遊戲
67-
# 開始遊戲
68-
guess_number_game()
69-
70-
</textarea>
39+
<textarea id="python-code" placeholder="在此輸入您的 Python 程式碼,例如:&#10;print('Hello, Pyodide!')">for i in range(3):
40+
user_input = input("請輸入字串")
41+
print(user_input + "!")</textarea>
7142
<br>
7243
<button id="run-button">執行程式碼</button>
7344
<h2>輸出結果:</h2>
@@ -84,35 +55,53 @@ <h2>輸出結果:</h2>
8455
let outputElement = document.getElementById("output");
8556
outputElement.textContent = "";
8657

87-
// 修改输出处理,添加换行符
58+
// 修改输出处理,实现即时显示
8859
let printOutput = (text) => {
89-
outputElement.textContent += text + "\n"; // 在每次输出后添加换行符
90-
outputElement.scrollTop = outputElement.scrollHeight;
60+
if (text && text.trim()) { // 只处理非空输出
61+
outputElement.textContent += text + "\n";
62+
outputElement.scrollTop = outputElement.scrollHeight;
63+
}
64+
};
65+
66+
// 将自定义的 prompt 函数添加到全局作用域
67+
globalThis.customPrompt = (message) => {
68+
printOutput(message); // 立即显示输入提示
69+
const result = prompt(message);
70+
if (result !== null) {
71+
printOutput(result + "!"); // 立即显示用户输入的结果
72+
}
73+
return result;
9174
};
9275

76+
// 修改 Python 的标准输出和错误输出处理
9377
pyodide.setStdout({
94-
batched: printOutput
78+
write: (text) => {
79+
printOutput(text);
80+
},
81+
flush: () => {}
9582
});
83+
9684
pyodide.setStderr({
97-
batched: printOutput
85+
write: (text) => {
86+
printOutput(text);
87+
},
88+
flush: () => {}
9889
});
9990

100-
// 实现交互式输入
91+
// 修改输入函数实现
10192
pyodide.runPython(`
102-
import sys
103-
from js import prompt
93+
from js import customPrompt
10494
105-
def custom_input(*args):
106-
return prompt("请输入:")
95+
def custom_input(prompt_text=""):
96+
return customPrompt(prompt_text)
10797
10898
input = custom_input
10999
`);
110100

111101
try {
112-
// 修改执行代码逻辑,直接执行整个代码块
113102
await pyodide.runPythonAsync(code);
114103
} catch (err) {
115-
outputElement.textContent += err;
104+
printOutput(err.toString());
116105
}
117106
}
118107

0 commit comments

Comments
 (0)