-1

我正在制作一个刽子手游戏并尝试创建一个函数来执行以下操作;

calculate_points(current_score,num_of_letter,letter_type):

现在当前分数=你有多少分。您通过猜测正确的辅音来获得分数,这是字符串中每个字母 +1 分(例如:'apples',a^^les 并且有人猜到 p,那将是 +2 分,它是辅音,但元音是相同的形式相反,您每个字母都会失去 -1 分。

CONSONANTS = 'bcdfghjklmnpqrstvwxyz'
VOWELS = 'aeiou'

字母类型可以是“C”或“c”表示辅音,“V”或“v”表示元音。当前分数从 0 开始,因此 current_score = 0,用户从 0 开始输入自己的分数。

那么例子:

calculate_points(2,3,'C') (had 2 points, guessed 3 correct letters that are consonants so + 1 point per correct guess) 2+ 3=5
5
calculate_points(3,2,'V') (had 3 points, guessed 2 correct letters that are vowels so that is (-1) points per correct guess, so 3-2 =1 
1

目前的尝试:

def calculate_score(current_score,num_of_letter,letter_type):

    new_score = 0

    for i in range(0,len(CONSONTANTS)):
       if CONSONANTS[i] == letter_type:
           new_score = current_score + (num_of_letter*1)
    for i in range(0,len(VOWELS)):
       if VOWELS[i] == letter_type:
           new_score = current_score + (num_of_letter*(-1))
    return new_score
4

1 回答 1

1

给定您想要的功能,您无需查看辅音和元音,这是一个简单的示例:

>>> def calculate_score(current_score, num_of_letter, letter_type):
    sign = 1 if letter_type == 'C' else -1
    return current_score + sign * num_of_letter

>>> calculate_score(2,3,'C')
5
>>> calculate_score(3,2,'V')
1
于 2012-10-19T10:34:39.183 回答