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)
Result:
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:
Currently, my program will print a score depending on what the user enters. After the first input, 'fazli' the score was 17.
My while loop is supposed to keep running until the user enters nothing.
The problem is my while loop I believe. It won't make a new score, it only copies the first one.
How can I keep running my while loop without it copying the first entered word score?
CodePudding user response:
because you don't update the score in the while loop. you have to recalculate it with each iteration. I changed it for you.
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:')
CodePudding user response:
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:')
CodePudding user response:
One can also build it as a function as below
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)