python - 拼字游戏基本分数
问题描述
我被要求编写一个程序,允许用户输入许多他喜欢的单词,计算单词的值并只打印最大值单词。所以,'a' 的值为 1,'aa' 的值为 2,我不知道如何对每个字母的分数求和。我懂了:
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)
但我收到错误,不知道如何继续,请帮助:(
解决方案
使用 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)
使用 For 循环
您需要遍历单词,然后遍历单词中的字母。您需要检查每个单词的分数与之前单词的最高分数。如果当前分数是最高的,那么这就是您需要打印的单词。
为了完成作业,这里是你如何去做的。
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)
第一次的输出是:
Input your words separated by ',' good, morning, bad, zulu, income
The word with the maximum score of : 13 is : zulu
第二次的输出是:
Input your words separated by ',' good, morning, bad, zulu, income, jack
The word with the maximum score of : 17 is : jack
推荐阅读
- node.js - 使用增量函数进行验证
- python - 通过用户输入在 CSV 文件中使用查询
- javascript - 要求 remoteParticipant 在房间视频通话中共享他们的屏幕之前获得批准 twilio
- anylogic - Anylogic,如何创建取决于每天到达代理数量的动态批量大小?
- java - gradlew bootRun 与 java -jar
- python - 我可以在英国开放天气地图中按邮政编码搜索吗
- jquery - 自定义事件在 Jquery 中触发,但找不到任何事件处理程序
- java - 不使用 JLayeredPane 将 jcomponent 移到后面
- python - from_point 函数不会在 windows CMD 上返回预期的控件
- eclipse - 为 Eclipse 安装 groovy 插件时出现问题