python - 我正在创建一个拼字游戏。用户正在输入他们拥有的字母,然后是他们想要创建的单词 如果可以创建单词,则返回 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,这应该意味着这个词可以拼写出来吗?由于某些原因,这些字母没有从单词列表中删除
解决方案
你打字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)
推荐阅读
- tensorflow - 如何解析 tfds.features.Sequence 对象?它与 tf.io.FixedLenSequenceFeature 不兼容
- javascript - 表单提交按钮中的表单同时触发
- javascript - 如何将表格行数据添加到数组
- odata - 我可以在“ZGWSAMPLE_BASIC”odata 服务中创建的表中插入数据吗?
- python - 从分钟值在熊猫数据框中添加日期时间列
- javascript - WooCommerce Checkout - 如果选中复选框,则需要输入
- python - 返回时如何修改表单?
- caching - Hazelcast 没有脑裂恢复(合并)
- javascript - 水平对齐按钮并显示活动按钮
- wordpress-theming - 在 site-nav.php 模板部分中包含 Gutenberg 的“搜索”块