18
18
}
19
19
# output {
20
20
width : 100% ;
21
- height : 200 px ;
21
+ height : 400 px ;
22
22
background-color : # f5f5f5 ;
23
23
padding : 10px ;
24
24
box-sizing : border-box;
36
36
</ head >
37
37
< body >
38
38
< h1 > Pyodide Python 執行器</ h1 >
39
- < textarea id ="python-code " placeholder ="在此輸入您的 Python 程式碼,例如: 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 程式碼,例如: print('Hello, Pyodide!') "> for i in range(3):
40
+ user_input = input("請輸入字串")
41
+ print(user_input + "!")</ textarea >
71
42
< br >
72
43
< button id ="run-button "> 執行程式碼</ button >
73
44
< h2 > 輸出結果:</ h2 >
@@ -84,35 +55,53 @@ <h2>輸出結果:</h2>
84
55
let outputElement = document . getElementById ( "output" ) ;
85
56
outputElement . textContent = "" ;
86
57
87
- // 修改输出处理,添加换行符
58
+ // 修改输出处理,实现即时显示
88
59
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 ;
91
74
} ;
92
75
76
+ // 修改 Python 的标准输出和错误输出处理
93
77
pyodide . setStdout ( {
94
- batched : printOutput
78
+ write : ( text ) => {
79
+ printOutput ( text ) ;
80
+ } ,
81
+ flush : ( ) => { }
95
82
} ) ;
83
+
96
84
pyodide . setStderr ( {
97
- batched : printOutput
85
+ write : ( text ) => {
86
+ printOutput ( text ) ;
87
+ } ,
88
+ flush : ( ) => { }
98
89
} ) ;
99
90
100
- // 实现交互式输入
91
+ // 修改输入函数实现
101
92
pyodide . runPython ( `
102
- import sys
103
- from js import prompt
93
+ from js import customPrompt
104
94
105
- def custom_input(*args ):
106
- return prompt("请输入:" )
95
+ def custom_input(prompt_text="" ):
96
+ return customPrompt(prompt_text )
107
97
108
98
input = custom_input
109
99
` ) ;
110
100
111
101
try {
112
- // 修改执行代码逻辑,直接执行整个代码块
113
102
await pyodide . runPythonAsync ( code ) ;
114
103
} catch ( err ) {
115
- outputElement . textContent += err ;
104
+ printOutput ( err . toString ( ) ) ;
116
105
}
117
106
}
118
107
0 commit comments