import random import string def read_file(file): with open(file,'r', encoding='UTF-8') as f: text = f.read() for ch in string.punctuation+string.digits: text = text.replace(ch," ") return text.split() def secret_word(ls): return random.choice(ls).lower() def get_guessed_word(cover_word, word, letter): result = "" for i in range(len(word)): if word[i] == letter: result += letter + " " else: result += cover_word[i2:i2+2] return result def word_guess(secret_word): guess_list=[] for i in range(len(secret_word)): guess_list.append('') cover_word = " ".join(guess_list) print("秘密单词是: {}".format(secret_word)) print("你的单词长度为 {} 个字符".format(len(secret_word))) limit_times = len(secret_word) * 2 print("你有 {} 次猜测机会,开始填词吧".format(limit_times)) i=1 while i<=limit_times: letter = input('请输入你猜测的字母:\n') if letter in secret_word: cover_word = get_guessed_word(cover_word, secret_word, letter) print("正确答案为:{}".format(cover_word)) if cover_word.find("") == -1: print("你太厉害了,居然只用了{}次就猜中了单词".format(i)) print("秘密单词是: {}".format(secret_word)) return secret_word else: print("真遗憾,你猜测的字母不在单词中!") i+=1 print("太遗憾了,你未能在{}次内猜出单词".format(limit_times)) print("秘密单词是: {}".format(secret_word)) return secret_word def main(): action = input() if action == "选词": random_seed = int(input()) random.seed(random_seed) word_list = read_file("data/dict.txt") secret_word = secret_word(word_list) print(secret_word) elif action == "模板": cover_word = input() word = input() letter = input() print(get_guessed_word(cover_word, word, letter)) elif action == "开始填词": random_seed = int(input()) random.seed(random_seed) word_list = read_file("data/dict.txt") secret_word = secret_word(word_list) word_guess(secret_word) else: print("加载单词信息") print("输入错误 ") if name == 'main': main()
时间: 2023-06-11 11:06:45 浏览: 98
这是一个Python程序,实现了一个单词猜猜看的小游戏。主要包含以下功能:
1. 读取文本文件中的单词列表,并随机选择一个单词作为秘密单词。
2. 根据秘密单词的长度,计算出玩家猜测的次数上限。
3. 玩家每次可以输入一个字母作为猜测,程序会告诉玩家这个字母是否在秘密单词中出现,并显示出已猜中的字母。
4. 当玩家猜中所有字母或达到了猜测次数上限时,程序会结束游戏并告诉玩家答案。
你可以通过输入 "选词" 来随机选择一个单词作为秘密单词,通过输入 "开始填词" 来开始游戏。如果你想测试程序的其他功能,可以输入 "模板" 来进行测试。
相关问题
with open(file_path, 'r', encoding='utf-8') as f:如何加入文件夹路径
您可以使用相对路径或绝对路径来指定文件夹路径。例如,如果您的文件夹路径是在当前工作目录下的一个名为“data”的文件夹中,您可以这样编写代码:
with open('data/'+file_path, 'r', encoding='utf-8') as f:
其中“data/”是文件夹路径,file_path是文件名。如果您的文件夹路径是绝对路径,例如“C:/Users/username/Documents/data/”,则可以这样编写代码:
with open('C:/Users/username/Documents/data/'+file_path, 'r', encoding='utf-8') as f:
请注意,不同操作系统的文件路径分隔符可能不同。在Windows系统中,路径分隔符为反斜杠“\”,而在Unix和Linux系统中,路径分隔符为正斜杠“/”。因此,为了确保代码的可移植性,建议使用os.path.join()函数来连接文件夹路径和文件名,例如:
import os
folder_path = 'data'
file_name = 'example.txt'
file_path = os.path.join(folder_path, file_name)
with open(file_path, 'r', encoding='utf-8') as f:
我的文件在D盘,怎么改写这段代码:with open(file_path, 'r', encoding='utf-8') as f:
你可以将 file_path 改为 'D:/文件名',例如:
```
with open('D:/文件名', 'r', encoding='utf-8') as f:
```
如果你的文件名中包含变量,你可以使用字符串格式化来构建文件路径,例如:
```
file_name = 'example.txt'
file_path = 'D:/' + file_name
with open(file_path, 'r', encoding='utf-8') as f:
```
或者使用 os 模块来构建文件路径,例如:
```
import os
file_name = 'example.txt'
file_path = os.path.join('D:', file_name)
with open(file_path, 'r', encoding='utf-8') as f:
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)