WORDS = ("python", "jumble", "easy", "difficult", "answer", "continue", "phone", "position", "position", "game") 1、对10个词进行循环猜词;每个词分别用随机、倒序、字母序三种顺序进行猜词;三种顺序分别用一个函数实现; 2、加入猜词结果的计分功能(猜词正确率),将每次需要猜测词和计分值作为参数传入函数,猜对计分值+1,否则-1; 3、在脚本的测试模式下运行程序(用main函数)
时间: 2023-07-06 16:31:11 浏览: 116
好的,我来分别回答你的三个问题:
1. 实现循环猜词和三种顺序的函数
```python
import random
WORDS = ("python", "jumble", "easy", "difficult", "answer", "continue", "phone", "position", "position", "game")
def random_order(word):
return "".join(random.sample(word, len(word)))
def reverse_order(word):
return word[::-1]
def alphabetical_order(word):
return "".join(sorted(word))
for word in WORDS:
for order_func in (random_order, reverse_order, alphabetical_order):
jumbled = order_func(word)
print("猜测单词:", jumbled)
guess = input("请输入你猜测的单词:")
if guess == word:
print("猜对了!\n")
else:
print("猜错了!\n")
```
2. 加入计分功能的函数
```python
import random
WORDS = ("python", "jumble", "easy", "difficult", "answer", "continue", "phone", "position", "position", "game")
def random_order(word):
return "".join(random.sample(word, len(word)))
def reverse_order(word):
return word[::-1]
def alphabetical_order(word):
return "".join(sorted(word))
def score_guess(guess, word, score):
if guess == word:
print("猜对了!\n")
return score+1
else:
print("猜错了!\n")
return score-1
def play_game(word, score):
for order_func in (random_order, reverse_order, alphabetical_order):
jumbled = order_func(word)
print("猜测单词:", jumbled)
guess = input("请输入你猜测的单词:")
score = score_guess(guess, word, score)
return score
def main():
score = 0
for word in WORDS:
score = play_game(word, score)
print("你的最终得分是:", score)
main()
```
3. 在测试模式下运行程序的main函数
```python
if __name__ == "__main__":
main()
```
完整代码如下:
阅读全文