- Python
python
import random
def game():
number_to_guess = random.randint(1, 100) yinanjinying.com
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太低了!')
elif guess > number_to_guess:
print('太高了!')
print(f'恭喜你,猜对了!数字是 {number_to_guess},你尝试了 {attempts} 次。')
if name == “main”:
game()
2. JavaScript (在浏览器中使用)
html
猜一个1到100之间的数字:
提交<script>
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let