WORDS = ("python", "jumble", "easy", "difficult", "answer", "continue", "phone", "position", "position", "game") 1、修改猜词程序,对10个词进行循环猜词;每个词猜词三次,分别为随机、倒序、字母序;三种顺序分别用一个函数实现; 2、加入猜词结果的计分功能(猜词正确率),将每次需要猜测词和计分值作为参数传入函数,猜对计分值+1,否则-1; 3、在脚本的测试模式下运行程序(用main函数)
时间: 2023-07-06 10:31:09 浏览: 136
好的,让我来解答你的问题:
首先,我明白你的要求。你想修改猜词程序,对10个词进行循环猜词,每个词猜词三次,分别为随机、倒序、字母序,三种顺序分别用一个函数实现;加入猜词结果的计分功能,将每次需要猜测词和计分值作为参数传入函数,猜对计分值+1,否则-1;最后,在脚本的测试模式下运行程序(用main函数)。
下面是代码实现:
```python
import random
WORDS = ("python", "jumble", "easy", "difficult", "answer", "continue", "phone", "position", "position", "game")
def random_order(word):
'''随机顺序'''
letters = list(word)
random.shuffle(letters)
return ''.join(letters)
def reverse_order(word):
'''倒序'''
return word[::-1]
def sorted_order(word):
'''字母序'''
return ''.join(sorted(word))
def guess_word(word, score):
'''猜词'''
print("The jumble is:", random_order(word))
guess = input("Your guess: ")
if guess == word:
print("Correct!\n")
score += 1
else:
print("Sorry, that's not it.")
print("The answer is:", word, "\n")
score -= 1
print("The jumble is:", reverse_order(word))
guess = input("Your guess: ")
if guess == word:
print("Correct!\n")
score += 1
else:
print("Sorry, that's not it.")
print("The answer is:", word, "\n")
score -= 1
print("The jumble is:", sorted_order(word))
guess = input("Your guess: ")
if guess == word:
print("Correct!\n")
score += 1
else:
print("Sorry, that's not it.")
print("The answer is:", word, "\n")
score -= 1
return score
def main():
score = 0
for word in WORDS:
print("The word is:", word)
for i in range(3):
score = guess_word(word, score)
print("Your final score is:", score)
if __name__ == '__main__':
main()
```
希望这个程序能够满足你的要求。如果还有其他问题,欢迎继续提问。
阅读全文