1

This isn't my exact code, but I'm basically doing this

    EnemyAttack = int(10)
    Move = input("| Cast an attack spell(1) | Cast a defense spell(2) |")
    if Move = ("2"):
       print ("You have casted a defense spell for 2 turns")
       EAN = 2
    if EAN >= 1:
       EnemyAttack = EnemyAttack / 2
       EAN = EAN - 1
    Health = Health - EnemyAttack

The code does halve the enemy's attack, however the output is not what i want it to be.

print ("You have been damaged for {}".format(EnemyAttack)

This displays

84.0 

I want it to output

84

I have already defined the EnemyAttack as an integer, so I'm confused why it's displayed as a real number. Any solutions? Also if you can, comment using a simple code that I can convert into my situation. Thanks.

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
Maxy Picky
  • 31
  • 1
  • 8
  • possible duplicate of [Format numbers to strings in Python](http://stackoverflow.com/questions/22617/format-numbers-to-strings-in-python) – Sk93 Jun 04 '15 at 09:18
  • If it's `EnemyAttack` that it's outputting, then why is it 84 instead of 10 or 5? Are you sure you've transferred your code correctly? – SuperBiasedMan Jun 04 '15 at 09:19
  • 1
    @SuperBiasedMan The 10 was an example, in the real code it was 84, sorry if it caused you any confusion – Maxy Picky Jun 04 '15 at 09:22

2 Answers2

2

You could format it without decimals, rounding it to the nearest whole number:

print("You have been damaged for {:.0f}".format(EnemyAttack))

: is a required separator; everything after it defines formatting instructions for that value. We use the f floating point formatter here, and .0 means zero digits after the decimal point. When you do that the decimal point itself is also dropped.

This has the advantage that the number will be rounded up as needed:

>>> EnemyAttack = 84.0
>>> print("You have been damaged for {:.0f}".format(EnemyAttack))
You have been damaged for 84
>>> EnemyAttack = 84.6
>>> print("You have been damaged for {:.0f}".format(EnemyAttack))
You have been damaged for 85

See the Formatting Specification Mini-Language section for the details.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • May i ask what the f is for? The code works perfectly now thanks. But what is the purpose of the f after the 0? – Maxy Picky Jun 04 '15 at 09:19
  • @MaxyPicky: I linked you to the documentation; everything after the `:` is the formatting specification (everything before is specifying what value to interpolate and format, omitting it uses auto-numbering). – Martijn Pieters Jun 04 '15 at 09:21
  • Quick, and maybe irrelevent, question, but if i place the {.0f} format where a string will take place, will an error occour? e.g.
        word = ("Hello") print ("He said {.0f}.".format(word))    
    – Maxy Picky Jun 04 '15 at 09:26
  • @MaxyPicky: yes, because string objects know nothing about the `f` formatting code. `str.format()` delegates formatting to the value itself, so `'Hello'.__format__('.0f')`, which fails. – Martijn Pieters Jun 04 '15 at 11:22
1

In Python you cannot define the type of a variabelname. If you want integer division, you have to use the integer division operator //:

EnemyAttack = EnemyAttack // 2
Daniel
  • 42,087
  • 4
  • 55
  • 81
  • +1 This is the root of the problem. My impression is the OP seems to believe that the {} format is not clever enough to recognise and correctly format an integer when it's passed one. – deStrangis Jun 04 '15 at 09:42