I'm trying to write a program for a small school project. It involves a use inputing two numbers one representing kills and one deaths.
The user inputs the numbers than the program should divide the kills by the number of deaths and put out the number. My problem is that the program isn't puting out decimal places. So 5/10 would just be 0 instead of 0.5. Here is the program

kills=int(raw_input("Please input the amount of kills: "))
deaths=int(raw_input("Please input the amount of deaths: "))

kills = int(kills)
deaths = int(deaths)

import math

print "You're K/D is", kills/deaths

Recommended Answers

All 8 Replies

Integer division returns an integer result. You must cast the integer to a float.

Something like this:

float(kills)

Worth noting: This was "fixed" in Python 3; 5/10 now gives 0.5

Worth noting: This was "fixed" in Python 3; 5/10 now gives 0.5

I have 2.6.1

Also thank you for the help

Worth noting: This was "fixed" in Python 3; 5/10 now gives 0.5

Correct me if i'm wrong but to do an integer division in python 3 don't you now do // rather than just the usual /.

Wait just checked it, to do integer division in python 3 you use the double slash.

Strangely enough Python always had the '/' and '//' operators, but it took Python30 to assign them appropriately. So now '/' is floating point and is '//' integer, before they were both integer division operators.

I fixed it this way.

import math

kills=int(raw_input("Please input the amount of kills: "))
deaths=int(raw_input("Please input the amount of deaths: "))

kills = int(kills)
deaths = int(deaths)

print "You're K/D is", float(kills)/float(deaths)

In C code we had only one divide, so if you wanted float out you had too divide floats.
By the way float(5.5)*int(1) = int(5)!

Lines 6 and 7 are redundant. In fact, in this particular bit of code, lines 3 and 4 are useless as well.

Scru, not my code. Cleaned up Skimy's code enough too run.
FYI pylint reports the following about the code.
************* Module test
C: 1: Missing docstring
C: 3: Operator not preceded by a space
kills=int(raw_input("Please input the amount of kills: "))
^
C: 3: Invalid name "kills" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C: 4: Operator not preceded by a space
deaths=int(raw_input("Please input the amount of deaths: "))
^
C: 4: Invalid name "deaths" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C: 6: Invalid name "kills" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C: 7: Invalid name "deaths" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
W: 1: Unused import math
Global evaluation
-----------------
Your code has been rated at -3.33/10

pylint is an RPM for linux, pylint-0.14.0-1.fc9.noarch.

commented: I never liked pylint; draconian piece of crap +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.