1. 质数检查器:探索数学奥秘
质数是只能被1和自身整除的数。
就像筛子一样,这个脚本会检查一个数字是否能被其他数字整除,如果不是,则它是质数。
# 质数检查器
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
num = int(input("请输入一个数字:"))
if is_prime(num):
print(num, "是质数")
else:
print(num, "不是质数")
2. 吊死鬼游戏:挑战你的词汇量
吊死鬼游戏是一个经典的猜词游戏。
# 吊死鬼游戏 (简化版)
word = "apple"
guesses = []
max_attempts = 6
while max_attempts > 0:
masked_word = ""
for letter in word:
if letter in guesses:
masked_word += letter
else:
masked_word += "_"
print(masked_word)
if masked_word == word:
print("恭喜你,你赢了!")
break
guess = input("猜一个字母:")
guesses.append(guess)
if guess not in word:
max_attempts -= 1
print("猜错了,你还有", max_attempts, "次机会")
if max_attempts == 0:
print("你输了,单词是:", word)
就像猜谜语一样,你根据提示逐步猜测单词,每次猜错都