Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将单词列表加载到3x3网格中?_Python_Grid - Fatal编程技术网

Python 如何将单词列表加载到3x3网格中?

Python 如何将单词列表加载到3x3网格中?,python,grid,Python,Grid,此代码打开一个包含10个单词的输入文件 import random weird_list=open("f:\\weird_list.txt", "r") wordlist=weird_list.read() #opens, reads and closes the text file. weird_list.close() wordlist=wordlist.split() 文件中的单词

此代码打开一个包含10个单词的输入文件

import random
weird_list=open("f:\\weird_list.txt", "r")    
wordlist=weird_list.read()                     #opens, reads and closes the    text file.
weird_list.close()                           
wordlist=wordlist.split()
文件中的单词有:夜、烟、鬼、牙、关于、骆驼、棕色、滑稽、椅子、价格

random1 = random.choice(wordlist)        #displays the random words one by one until it opens 9 out of 10.    
wordlist.remove(random1)            

random2 = random.choice(wordlist)  #it does this by removing the word from the file.
wordlist.remove(random2)

random3 = random.choice(wordlist)
wordlist.remove(random3)

random4 = random.choice(wordlist)
wordlist.remove(random4)

random5 = random.choice(wordlist)
wordlist.remove(random5)

random6 = random.choice(wordlist)
wordlist.remove(random6)

random7 = random.choice(wordlist)
wordlist.remove(random7)

random8 = random.choice(wordlist)
wordlist.remove(random8)

random9 = random.choice(wordlist)
wordlist.remove(random9)
它向玩家解释游戏规则

print ("Let's play guess the word \n I have a random list of words \n the computer will select randomly from the word list leaving you one remaining word to guess.")
这将打印(显示)单词

print ("I will give you the following words, you then tell me the missing   word: \n", random1, random2, random3, random4, random5, random6, random7,   random8,   random9,)

您可以使用列表理解:

wordsstr =''.join(str(x) for x in wordlist)

weirdguess = input("what is the missing word?: ")

while weirdguess != wordsstr: 
   weirdguess == input ("try another word: ")


print ("You guessed correctly. Game over") #ends the while loop
输出:

import random
weird_list=open("f:\\weird_list.txt", "r")    
wordlist=weird_list.read() #opens, reads, and closes the text file.
weird_list.close()                           
wordlist=wordlist.split()
random.shuffle(wordlist)
wordstr = wordlist[0]
wordlist = wordlist[1:]
print("\n".join(" ".join(wordlist[x:x+3]) for x in range(0, len(wordlist), 3)))

print("Correct answer: %s" % wordstr)

注意,我还更改了你的随机化器
random.shuffle()
比自己用
random.choice()

来洗牌要容易得多。您可以使用列表理解:

wordsstr =''.join(str(x) for x in wordlist)

weirdguess = input("what is the missing word?: ")

while weirdguess != wordsstr: 
   weirdguess == input ("try another word: ")


print ("You guessed correctly. Game over") #ends the while loop
输出:

import random
weird_list=open("f:\\weird_list.txt", "r")    
wordlist=weird_list.read() #opens, reads, and closes the text file.
weird_list.close()                           
wordlist=wordlist.split()
random.shuffle(wordlist)
wordstr = wordlist[0]
wordlist = wordlist[1:]
print("\n".join(" ".join(wordlist[x:x+3]) for x in range(0, len(wordlist), 3)))

print("Correct answer: %s" % wordstr)

注意,我还更改了你的随机化器
random.shuffle()
比自己用
random.choice()

洗牌要容易得多,我想你需要解释一下3x3网格是什么意思。您使用的是什么数据结构?如何使用3x3网格?我认为您需要解释3x3网格的含义。您使用的是什么数据结构?如何使用3x3网格?