A word game awards points for the letters used in a word. The lower the frequency of the letter in the English language, the higher the score for the letter. Write a program that asks the user to input a word. The program should then output the score for the word according to the following rules:
I have tied written two pieces of code for this task I want to know if there are any other more logic and simple ways of doing this.
First Try:
#Letter Game Challenge
letters = ("e","a","r","i","o","t","n","s","l","c","u","d","p","m","h"
,"g","b","f","y","w","k","v","x","z","j","q")
points = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23
,24,25,26)
def main():
global word_input
print ("Input a word to see the score")
word_input = input()
if any(letter in word_input for letter in letters):
l1()
else:
print ("Enter a word with letters in!")
main()
def l1():
global score
global lcheck
lcheck = word_input
score = 0
while word_input != "":
if lcheck == "":
break
if any(letter in lcheck for letter in "e"):
num = lcheck.count("e")
score = score + 1*num
lcheck = lcheck.replace("e", "")
if any(letter in lcheck for letter in "a"):
num = lcheck.count("a")
score = score + 2*num
lcheck = lcheck.replace ("a", "")
if any(letter in lcheck for letter in "r"):
num = lcheck.count("r")
lcheck = lcheck.replace("r", "")
score = score + 3*num
if any(letter in lcheck for letter in "i"):
num = lcheck.count("i")
lcheck = lcheck.replace("i", "")
score = score + 4*num
if any(letter in lcheck for letter in "o"):
num = lcheck.count("o")
lcheck = lcheck.replace("o", "")
score = score + 5*num
if any(letter in lcheck for letter in "t"):
num = lcheck.count("t")
lcheck = lcheck.replace("t", "")
score = score + 6*num
if any(letter in lcheck for letter in "n"):
num = lcheck.count("n")
lcheck = lcheck.replace("n", "")
score = score + 7*num
if any(letter in lcheck for letter in "s"):
num = lcheck.count("s")
lcheck = lcheck.replace("s", "")
score = score + 8*num
if any(letter in lcheck for letter in "l"):
num = lcheck.count("l")
lcheck = lcheck.replace("l", "")
score = score + 9*num
if any(letter in lcheck for letter in "c"):
num = lcheck.count("c")
lcheck = lcheck.replace("c", "")
score = score + 10*num
if any(letter in lcheck for letter in "u"):
num = lcheck.count("u")
lcheck = lcheck.replace("u", "")
score = score + 11*num
if any(letter in lcheck for letter in "d"):
num = lcheck.count("d")
lcheck = lcheck.replace("d", "")
score = score + 12*num
if any(letter in lcheck for letter in "p"):
num = lcheck.count("p")
lcheck = lcheck.replace("p", "")
score = score + 13*num
if any(letter in lcheck for letter in "m"):
num = lcheck.count("m")
lcheck = lcheck.replace("m", "")
score = score + 14*num
if any(letter in lcheck for letter in "h"):
num = lcheck.count("h")
lcheck = lcheck.replace("h", "")
score = score + 15*num
if any(letter in lcheck for letter in "g"):
num = lcheck.count("g")
lcheck = lcheck.replace("g", "")
score = score + 16*num
if any(letter in lcheck for letter in "b"):
num = lcheck.count("b")
lcheck = lcheck.replace("b", "")
score = score + 17*num
if any(letter in lcheck for letter in "f"):
num = lcheck.count("f")
lcheck = lcheck.replace("f", "")
score = score + 18*num
if any(letter in lcheck for letter in "y"):
num = lcheck.count("y")
lcheck = lcheck.replace("y", "")
score = score + 19*num
if any(letter in lcheck for letter in "w"):
num = lcheck.count("w")
lcheck = lcheck.replace("w", "")
score = score + 20*num
if any(letter in lcheck for letter in "k"):
num = lcheck.count("k")
lcheck = lcheck.replace("k", "")
score = score + 21*num
if any(letter in lcheck for letter in "v"):
num = lcheck.count("v")
lcheck = lcheck.replace("v", "")
score = score + 22*num
if any(letter in lcheck for letter in "x"):
num = lcheck.count("x")
lcheck = lcheck.replace("x", "")
score = score + 23*num
if any(letter in lcheck for letter in "z"):
num = lcheck.count("z")
lcheck = lcheck.replace("z", "")
score = score + 24*num
if any(letter in lcheck for letter in "j"):
num = lcheck.count("j")
lcheck = lcheck.replace("j", "")
score = score + 25*num
if any(letter in lcheck for letter in "q"):
num = lcheck.count("q")
lcheck = lcheck.replace("q", "")
score = score + 26*num
print ("The score for that word is ",score)
print ()
check()
def check():
print ("Do you want to go again?")
again = input().lower
if again == "y" or again == "yes":
print()
main()
if again == "no" or again == "n":
print ()
print ("Bye!")
else:
print ()
print ("Enter a valid input!")
print ()
check()
main()
Second Try:
#Letter game challenge
letters = ("e","a","r","i","o","t","n","s","l","c","u","d","p","m","h",
"g","b","f","y","w","k","v","x","y","z")
gained_point = 0
def main():
global word_input
print ("Input a word to see the score")
word_input = str(input())
l1(gained_point)
def l1(gained_point):
score = 0
linword = len([ltr for ltr in word_input if ltr.isalpha()])
linword = linword - 1
while linword > -1:
if linword < 0:
break
position = letters.index(word_input[linword])
linword = linword - 1
gained_point = gained_point + position
gained_point = gained_point + 1
print ("The score for that word is",gained_point)
main()
答案 0 :(得分:2)
letters = ("e","a","r","i","o","t","n","s","l","c","u","d","p","m","h" ,"g","b","f","y","w","k","v","x","z","j","q")
points = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 ,24,25,26)
word = 'nThsnth'
letter_points = dict(zip(letters, points))
total = sum(letter_points.get(c, 0) for c in word.lower())