关于for循环数学的基本python函数

2024-06-16 10:11:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个刽子手游戏,并试图创建一个函数来 执行以下操作:

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开始,所以当前分数=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

Tags: ofnewthattype字母currentnum分数
1条回答
网友
1楼 · 发布于 2024-06-16 10:11:02

你不需要考虑辅音和元音的功能,这里有一个简单的例子:

>>> 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

相关问题 更多 >