简体   繁体   English

拼字游戏基本分数

[英]Scrabble basic score

i have been asked to write a program that allows a user to enter many words as he likes, calculates the value of the words and only print the max value word.我被要求编写一个程序,允许用户输入他喜欢的多个单词,计算单词的值并只打印最大值的单词。 So, 'a' would have a value of 1 and 'aa' a value of 2, i dont know how to sum the scores of each letter.所以,'a' 的值为 1,'aa' 的值为 2,我不知道如何对每个字母的分数求和。 i got this:我懂了:

word = input("Input your word ' ")

words = word.split()

score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, 
          "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, 
          "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, 
          "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, 
          "x": 8, "z": 10}

total=score[words]

print(total)

but i get error and dont know how to continue, help please :(但我收到错误,不知道如何继续,请帮忙:(

Using While Loop使用 While 循环

word = input("Input your words separated by ',' ")

words = word.split(',')

score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, 
          "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, 
          "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, 
          "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, 
          "x": 8, "z": 10}

max_score = 0
max_score_word = ''

wd_len = len(words)
word_count = 0
while word_count < wd_len: #you need to iterate thru each word
    total = 0  #reset counter to calculate score for each word
    wd = words[word_count].lower() #store the current word to process
    letter_len = len(wd) #find the length of the word
    letter_count = 0 #reset counter to iterate thru the word
    while letter_count < letter_len: #iterate thru each letter in the word
        letter = wd[letter_count]
        if letter in score: #check if letter is alphabet
            total = total + score[letter] #add to total
        letter_count +=1 #increase counter for letter to process next letter
    if max_score < total: #check if this score is a new high score
        max_score = total #reset high score with current score
        max_score_word = wd #keep track of the word with the highest score
    word_count +=1 #increase counter for word to process next word

#once you are done with everything, print the word and score
        
print('The word with the maximum score of :',max_score, 'is : ',max_score_word)

Using For Loop使用 For 循环

You need to iterate through the words and then thru the letters in the word.您需要遍历单词,然后遍历单词中的字母。 You need to check the score of each word against the max score of previous words.您需要根据前一个单词的最大分数检查每个单词的分数。 If the current score is the highest, then that's the word you need to print.如果当前分数是最高的,那么这就是您需要打印的单词。

To accomplish the homework, here's how you go about doing it.要完成作业,请按照以下步骤进行操作。

word = input("Input your words separated by ',' ")

words = word.split(',')

score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, 
          "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, 
          "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, 
          "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, 
          "x": 8, "z": 10}

max_score = 0
max_score_word = ''

for wd in words: #you need to iterate thru each word
    total = 0  #reset counter to calculate score for each word
    for letter in wd: #iterate thru each letter in the word
        if letter.lower() in score: #check if letter is alphabet
            total = total + score[letter.lower()] #add to total
    if max_score < total: #check if this score is a new high score
        max_score = total #reset high score with current score
        max_score_word = wd #keep track of the word with the highest score

#once you are done with everything, print the word and score
        
print('The word with the maximum score of :',max_score, 'is : ',max_score_word)

The output for the first time was:第一次输出是:

Input your words separated by ',' good, morning, bad, zulu, income
The word with the maximum score of : 13 is :   zulu

The output for the second time was:第二次的输出是:

Input your words separated by ',' good, morning, bad, zulu, income, jack
The word with the maximum score of : 17 is :   jack

try this:尝试这个:

word = input("Input your words separated by ','")
words = word.split(',')
print(words)
score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, "x": 8, "z": 10}
biggest = ''
total_max = 0
for singleword in words:
    total = 0
    for letter in singleword:
        total += score[letter]
    if total > total_max:
        biggest = singleword
        total_max = total
print(f'{biggest=} {total_max=}')
**EXAMPLE:**
Input your words separated by ','
this is a test for biggest word
['this', 'is', 'a', 'test', 'for', 'biggest', 'word']
biggest='biggest' total_max=11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM