CodeWar打怪升级-Python篇

本文介绍了一个Python编程练习题,涉及将字符串转换为新字符串,新字符串中每个字符仅出现一次时用'('表示,多次出现时用')'表示。此外,还讲解了如何找到并求和两个最小的数字。文章包括大小写转换、循环、replace函数等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.  The goal of this exercise is to convert a string to a new string where each character in the new string is "(" if that character appears only once in the original string, or ")" if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate.

Examples

"din"      =>  "((("
"recede"   =>  "()()()"
"Success"  =>  ")())())"
"(( @"     =>  "))((" 

My answer

大小写转换函数、for循环、replace函数

def duplicate_encode(word):
    
    count={}
    for i in word.lower():
        if i not in count:
            count[i]=1
        else:
            count[i]+=1
    
    new_word=word.lower()
    for i in new_word:
        if count[i]>1:
            new_word=new_word.replace(i,')')
        else:
            new_word=new_word.replace(i,'(')
    return new_word

  

 2

 

 

 

 

 

def sum_two_smallest_numbers(numbers):
    for i in range(0,len(numbers)-1):
        if (not isinstance(numbers[i],int)) or numbers[i]<0:
            numbers.remove(numbers[i])
    new_num=numbers.sort()
    sum=0
    sum=sum+int(new_num[0])+int(new_num[1])
    return sum

  

 

 

 
posted on 2019-06-12 10:42  Suckseedeva 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/skyEva/p/11008185.html

以下是一个简单的主角打怪升级的代码示例,使用Python语言实现: ```python import random player_level = 1 player_exp = 0 player_max_exp = 10 player_hp = 100 player_max_hp = 100 player_attack = 10 monster_level = 1 monster_hp = 50 monster_max_hp = 50 monster_attack = 5 print("你遇到了一个等级为", monster_level, "的怪物,准备战斗!") while player_hp > 0 and monster_hp > 0: # 玩家击怪物 damage = random.randint(player_attack - 2, player_attack + 2) monster_hp -= damage print("你攻击了怪物,造成了", damage, "点伤害。怪物剩余血量:", monster_hp) # 怪物攻击玩家 damage = random.randint(monster_attack - 2, monster_attack + 2) player_hp -= damage print("怪物攻击了你,造成了", damage, "点伤害。你剩余血量:", player_hp) # 判断战斗结果 if player_hp <= 0: print("你被怪物击败了,游戏结束。") break if monster_hp <= 0: print("你击败了怪物,获得了", monster_level * 10, "点经验值。") player_exp += monster_level * 10 if player_exp >= player_max_exp: player_level += 1 player_max_exp *= 2 player_hp = player_max_hp player_attack += 5 print("恭喜你升级了!当前等级为", player_level, ",最大生命值增加到", player_max_hp, ",攻击力增加到", player_attack, "。") # 生成新的怪物 monster_level += 1 monster_max_hp *= 2 monster_hp = monster_max_hp monster_attack += 5 print("你遇到了一个等级为", monster_level, "的怪物,准备战斗!") ``` 在这个代码中,玩家和怪物都有等级、生命值、攻击力等属性。玩家和怪物每次攻击都会随机产生一定的伤害值,直到一方的生命值降为0为止。如果玩家胜利,就会获得一定的经验值,并有可能升级升级后,玩家的属性会得到提升,同时会生成一个等级更高的新怪物。如果玩家失败,则游戏结束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值