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.

3
  • possible duplicate of Format numbers to strings in Python Commented Jun 4, 2015 at 9: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? Commented Jun 4, 2015 at 9:19
  • 1
    @SuperBiasedMan The 10 was an example, in the real code it was 84, sorry if it caused you any confusion Commented Jun 4, 2015 at 9:22

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
@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).
Quick, and maybe irrelevent, question, but if i place the {.0f} format where a string will take place, will an error occour? e.g.<pre> word = ("Hello") print ("He said {.0f}.".format(word)) <code>
@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.
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 

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.