jupyter notebook有趣的代码
时间: 2023-10-16 18:07:12 浏览: 121
以下是一个有趣的代码,可以在jupyter notebook中运行:
```python
from IPython.display import display, Javascript
from random import randint
def play_game():
number = randint(1, 100)
guess = -1
while guess != number:
guess = int(input("Guess the number (between 1 and 100): "))
if guess < number:
print("Too low!")
elif guess > number:
print("Too high!")
print("You win!")
display(Javascript('''
var r = confirm("Do you want to play a game?");
if (r == true) {
''' + play_game.__code__.co_body.decode('utf-8') + '''
}
'''))
```
这个代码会弹出一个对话框,询问用户是否想要玩一个猜数字的游戏。如果用户点击了“确定”,则会开始游戏。用户需要在输入框中输入一个数字,程序会告诉用户这个数字是太高了还是太低了,直到用户猜中了这个数字为止。
阅读全文
相关推荐













