user_input = input('Welcome to the Scribble Scorer; Enter Word:')
i = {'e': 1, 't': 1.5, 'a': 2, 'i': 2, 'n': 2, 's': 2, 'h': 2.5, 'r': 2.5, 'd': 2.5, 'l': 3, 'u': 3, 'c': 3.5, 'm': 3.5, 'f': 4, 'w': 4.5, 'y': 4.5, 'p': 5, 'g': 5, 'b': 5.5, 'v': 5.5, 'k': 5.5, 'q': 5.5, 'j': 6, 'x': 6, 'z':6}
totalValue = 0
for char in user_input: # For every character in our string.
if char in i: # If this character has a defined weight value.
totalValue = i[char] # Add it to our total.
print('Word:', user_input, 'Score:', totalValue)
while user_input != 'nothing':
user_input = input('Welcome to the Scribble Scorer; Enter Word:')
if user_input != 'nothing':
print('Word:', user_input, 'Score:', totalValue)
結果:
Welcome to the Scribble Scorer; Enter Word: fazli
Word: fazli Score: 17
Welcome to the Scribble Scorer; Enter Word: mia
Word: mia Score: 17
Welcome to the Scribble Scorer; Enter Word:
目前,我的程式將根據用戶輸入的內容列印分數。第一次輸入后,'fazli' 得分為 17。
我的 while 回圈應該一直運行,直到用戶不輸入任何內容。
問題是我相信我的 while 回圈。它不會產生新的分數,它只會復制第一個分數。
如何在不復制第一個輸入的單詞分數的情況下繼續運行我的 while 回圈?
uj5u.com熱心網友回復:
因為你沒有在 while 回圈中更新分數。您必須在每次迭代時重新計算它。我給你改了。
i = {'e': 1, 't': 1.5, 'a': 2, 'i': 2, 'n': 2, 's': 2, 'h': 2.5, 'r': 2.5, 'd': 2.5, 'l': 3, 'u': 3, 'c': 3.5, 'm': 3.5, 'f': 4, 'w': 4.5, 'y': 4.5, 'p': 5, 'g': 5, 'b': 5.5, 'v': 5.5, 'k': 5.5, 'q': 5.5, 'j': 6, 'x': 6, 'z':6}
user_input = input('Welcome to the Scribble Scorer; Enter Word:')
while user_input != 'nothing':
totalValue = 0
for char in user_input: # For every character in our string.
if char in i: # If this character has a defined weight value.
totalValue = i[char] # Add it to our total.
print('Word:', user_input, 'Score:', totalValue)
user_input = input('Welcome to the Scribble Scorer; Enter Word:')
uj5u.com熱心網友回復:
i = {'e': 1, 't': 1.5, 'a': 2, 'i': 2, 'n': 2, 's': 2, 'h': 2.5, 'r': 2.5, 'd': 2.5, 'l': 3, 'u': 3, 'c': 3.5, 'm': 3.5, 'f': 4, 'w': 4.5, 'y': 4.5, 'p': 5, 'g': 5, 'b': 5.5, 'v': 5.5, 'k': 5.5, 'q': 5.5, 'j': 6, 'x': 6, 'z':6}
#take user input
user_input = input('Welcome to the Scribble Scorer; Enter Word:')
#check if its not 'nothing'
while user_input != 'nothing':
totalValue = 0
#compute the score
for char in user_input:
if char in i:
totalValue = i[char]
print('Word:', user_input, 'Score:', totalValue)
#get user input again for next iteration
user_input = input('Welcome to the Scribble Scorer; Enter Word:')
uj5u.com熱心網友回復:
也可以將其構建為如下功能
i = {'e': 1, 't': 1.5, 'a': 2, 'i': 2, 'n': 2, 's': 2, 'h': 2.5, 'r': 2.5, 'd': 2.5, 'l': 3, 'u': 3, 'c': 3.5,
'm': 3.5, 'f': 4, 'w': 4.5, 'y': 4.5, 'p': 5, 'g': 5, 'b': 5.5, 'v': 5.5, 'k': 5.5, 'q': 5.5, 'j': 6, 'x': 6, 'z': 6}
def calculate_score(word: str):
totalValue = 0
for char in word: # For every character in our string.
if char in i: # If this character has a defined weight value.
totalValue = i[char] # Add it to our total.
return totalValue
user_input = ''
while user_input != 'nothing':
user_input = input('Welcome to the Scribble Scorer; Enter Word:')
if user_input != 'nothing':
totalValue = calculate_score(user_input)
print('Word:', user_input, 'Score:', totalValue)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/344557.html
上一篇:當所有運算式都在regex101.com上作業時,Python正則運算式不起作用,當試圖將8"W->8"W和8"otherword->8"其他單詞
