Creating python scrabble function -


i'm trying create scrabble function takes string of letters , returns score based on letters. code far:

def scrabble_score(rack):     count = 0     letter in rack:         if letter == "eaionrtlsu":             count += 1             return count         elif letter == "dg":             count += 2             return count         elif letter == "bcmp":             count += 3             return count                 elif letter == "fhvwy":             count += 4             return count         elif letter == "k":             count += 5             return count         elif letter == "jx":             count += 8             return count         else:             letter == "qz"             count += 10             return count 

however when tried call function

    scrabble_score("aabbwol") 

it returns 10 when should return 14?

the code has 2 issues:

  • the return statement should called 1 time, after for iteration ends, can sum letter values
  • each if statement should check if letter in string, not if equal list of letters specific value.

then like:

def scrabble_score(rack):     count = 0     letter in rack:         print letter         if letter in "eaionrtlsu":             count += 1         elif letter in "dg":             count += 2         elif letter in "bcmp":             count += 3         elif letter in "fhvwy":             count += 4         elif letter in "k":             count += 5         elif letter in "jx":             count += 8         else:             count += 10     return count 

returning: 14


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -