我正在制作一个刽子手游戏并尝试创建一个函数 执行以下操作;
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
答案 0 :(得分: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