【问题标题】:I'm creating a scrabble game. The user is inputting the letters they have and then the word they want to create Return true if the word can be made我正在创建一个拼字游戏。用户正在输入他们拥有的字母,然后是他们想要创建的单词 如果可以创建单词,则返回 true
【发布时间】:2021-09-08 22:47:22
【问题描述】:

`

#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,这应该意味着这个词可以拼写出来吗?由于某些原因,这些字母没有从单词列表中删除

【问题讨论】:

    标签: python coding-style


    【解决方案1】:

    您输入的是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)
    

    【讨论】:

      猜你喜欢
      • 2021-07-14
      • 2022-01-12
      • 1970-01-01
      • 2022-06-10
      • 2018-06-25
      • 2016-05-21
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      相关资源
      最近更新 更多