Python--用循环完成一个猜单词的小游戏

运用random模块

random.choice(words)

random.randrange()

len()全局函数

使用切片

[参数1:参数2:step]

import random
WORDS = ("python","import","hello","difficult","easy")
print("欢迎来到猜单词游戏,请将乱序后的单词组成正确的单词")
iscontinue = "y"
while iscontinue == "y" or iscontinue == "Y" or iscontinue == "yes" or iscontinue == "YES":
	words = random.choice(WORDS)
	right = words
	print(words)
	newwords = ""
	while words:
		position = random.randrange(len(words))
		newwords += words[position]
		words = words[:position] + words[position + 1:]
	print("乱序后的单词是:",newwords)
	guess = input("请你猜单词:")
	while guess != right and guess != "":
		print("抱歉,你猜错了!")
		guess = input("请你继续猜:")
	if guess == right:
		print("恭喜你猜对了!")
		iscontinue = input("你是否继续游戏Y/N:")

 

你可能感兴趣的:(python)