首页 > 解决方案 > 我正在创建一个拼字游戏。用户正在输入他们拥有的字母,然后是他们想要创建的单词 如果可以创建单词,则返回 true

问题描述

`

#Problem 1
# #inputs
rack=input("what are the characters in your rack?: ")
word=input("what word would you like to create?: ")
#converting the rack to list
rack_list=[]
rack_list[:0]=rack
#Converting word to list
word_list=[]
word_list[:0]=word
#deleting whatever letters word has in it from rack list
for char in rack_list:
    if char ==word_list:
        word_list.remove(char)
#seeing if there are no more letters left in wordlist the word can be created

if len(word_list)==0:
    can_make_word_bool=True
else:
    can_make_word_bool=False    

print(can_make_word_bool)

` can_make_word_bool 值将在单词可以生成时返回 true,如果不能生成则返回 false。我这样做是为了如果用户在机架列表中有这些字母,单词中的任何字母都会从单词列表中删除。如果 word_list 的长度在末尾为 0,这应该意味着这个词可以拼写出来吗?由于某些原因,这些字母没有从单词列表中删除

标签: pythoncoding-style

解决方案


你打字if char == word_list:,不是if char in word_list:。下面是代码的样子:

#Problem 1
# #inputs
rack=input("what are the characters in your rack?: ")
word=input("what word would you like to create?: ")
#converting the rack to list
rack_list=[]
rack_list[:0]=rack
#Converting word to list
word_list=[]
word_list[:0]=word
print(word_list)
#deleting whatever letters word has in it from rack list
for char in rack_list:
    if char in word_list: ### EDITED HERE
        word_list.remove(char)
        print(word_list)
#seeing if there are no more letters left in wordlist the word can be created

if len(word_list)==0:
    can_make_word_bool=True
else:
    can_make_word_bool=False    

print(can_make_word_bool)

推荐阅读