| import gradio as gr | |
| import random | |
| mole_pos = random.randint(1, 9) | |
| def whack(choice): | |
| global mole_pos | |
| result = "打中了!" if int(choice) == mole_pos else "沒打中!" | |
| mole_pos = random.randint(1, 9) | |
| return result | |
| iface = gr.Interface(fn=whack, | |
| inputs=gr.Radio(choices=[str(i) for i in range(1, 10)], label="選擇洞口"), | |
| outputs="text", | |
| title="打地鼠遊戲", | |
| description="點選你想打的洞口!") | |
| iface.launch() | |