【问题标题】:How i can print difference with percentage我如何用百分比打印差异
【发布时间】:2019-10-20 14:51:01
【问题描述】:

我正在为学校做文字游戏练习,我们必须为它制作四个不同的功能。我已经完成了健康点系统,现在我想打印玩家使用了多少百分比。

这是我的 hp 系统:

def hela(player_health, hit):
    return player_health - hit

player_health_points = 15
dog_hit_value = 4
pistol_hit_value = 15
brasnuckles_hit_value = 3
punch_hit_value = 1
kick_hit_value = 2
player_health_points = hela(player_health_points, dog_hit_value,)
player_health_points = hela(player_health_points, brasnuckles_hit_value)
print('You have ' + str(player_health_points) + ' HP left!')*

这就是我现在所做的,并因此失去了休息的大脑:

def hp_left_percentage(x,y):
    vastaus = x - y
    return vastaus

health = 16
tulos = hp_left_percentage(health,player_health_points)
print(tulos)

问题:

  • 为什么我必须为 def hp_left_percentage health 添加一个才能显示正确的值?

  • 现在 def hp_left_percentage 显示玩家有多少生命值,以及数值。我如何获得功能打印百分比?

希望你能明白我的意思:)

【问题讨论】:

    标签: python function percentage


    【解决方案1】:

    如果我正确理解您的问题,您想显示剩余生命值的百分比,而不仅仅是绝对数字?现在你的health_left_percentage 只显示HP 的数字差异。要计算百分比,您可以将其更改为:

    def hp_left_percentage(x,y):
        # I assume, 'y' is the damage here.
    
        vastaus = (x - y) / x
        print(f'You have {vastaus:.0%} HP left!')
    

    这样,函数将自动计算百分比并以正确的格式返回。我建议阅读 f-strings 以获得一种简洁的方式来以您希望的方式显示带有变量的字符串。

    希望这会有所帮助。如果我误解了你,请告诉我。

    编辑 如果玩家被击中两次或更多次,那么你应该将初始生命值存储在一个全局变量中并像这样更改函数:

    initial_hp = 100
    
    
    def hp_left_percentage(x, y):
        vastaus = (x - y) / initial_hp
        return vastaus
    
    
    perc_left = hp_left_percentage(x, y)
    print(f'You have {perc_left:.0%} HP left!')
    

    【讨论】:

    • 这表示您的健康状况刚刚下降了多少,而不是从初始值剩余的百分比。
    • 怎么样?如果x=100,初始HP,y=30,伤害,函数返回70%,原样。
    • 那么如果你再失去 20 个,它应该显示你现在有 50%(如果我猜对了 OP 的意图)。您的代码将显示 71%
    • 当然,为了使函数有意义,它应该 return 值,而不仅仅是打印它。
    • 所以,在我的例子中,x = player_health (15) 和 y = player_health_points。为什么我不能只写 x - y / x ?那个代码仍然告诉我 47% 这意味着花了多少钱,而不是剩下多少?对不起,如果我误解了什么。两个月前我开始上学,在那之前我是 html 新手,对 python 一无所知。
    【解决方案2】:

    你的意思是这样的吗?

    def hp_left_percentage(x,y):
        vastaus = x - y
        percentage = '{0:.2f}'.format((vastaus / x * 100))
        return vastaus
    

    【讨论】:

    • def hela(player_health, hit): return player_health - hit def hp_left_percentage(x, y):vastaus = 1 - (x - y) / initial_hp returnvastaus player_health_points = 15 dog_hit_value = 4 player_health = 15 initial_hp = 15 player_health_points = hela(player_health_points, dog_hit_value,) perc_left = hp_left_percentage(player_health, player_health_points) print(f'你还有 {perc_left:.2%} HP!')
    【解决方案3】:

    我在度假,所以没有在这里写。感谢所有的答案。

    这就是我所做的。

    def hela(player_health, hit):
        return player_health - hit
    
    def hp_left_percentage(x, y):
        vastaus = 1 - (x - y) / initial_hp
        return vastaus
    
    player_health_points = 15
    
    dog_hit_value = 4
    
    player_health = 15
    initial_hp = 15
    
    player_health_points = hela(player_health_points, dog_hit_value,)
    
    perc_left = hp_left_percentage(player_health, player_health_points)
    
    print(f'You have {perc_left:.2%} HP left!')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 2015-05-09
      • 2011-07-15
      • 1970-01-01
      • 2021-06-17
      • 2021-01-05
      • 1970-01-01
      相关资源
      最近更新 更多