0

I am trying to write a python script that divides two numbers and then stores the value of the first digit after the decimal point but first I want to print the answer of the division. example: 31/7 should be 4,428 but when I use

def spiel():
    anzahl=int(raw_input("Anzahl der Spieler"))
    spieler=int(raw_input("Jeder wievielte spielt"))
    if anzahl>0 and spieler>0:
#Quotient berechnen
        q=anzahl/spieler
        print '%.2f'%q

I get the answer 4.00, what have I done wrong.

If you could tell me how I can store the first decimal point after the decimal point THAT WOULD BE AMAZING! Thanks in advance

turbulencetoo
  • 3,447
  • 1
  • 27
  • 50
  • 2
    Possible duplicate of [How can I force division to be floating point in Python?](http://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-in-python) – Moses Koledoye Aug 11 '16 at 19:55

4 Answers4

0

You're dividing an int by an int. You should use floats

def spiel():
    anzahl=float(raw_input("Anzahl der Spieler"))
    spieler=float(raw_input("Jeder wievielte spielt"))
    if anzahl>0 and spieler>0:
#Quotient berechnen
        q=anzahl/spieler
        print '%.2f'%q

In response to comment:

There are multiple ways to do that, what's best would depend on your applicaiton:

q =anzahl/spieler
print "{}".format(int(10*(q%1)))
print "{}".format(int((q-int(q))*10))
print "{}".format(str(a).split(".")[1][0])

and probably some easier ones that I didn't think of

Ben
  • 6,986
  • 6
  • 44
  • 71
0

In python2.7: If you divide two integer, answer will be integer always as it will skip the decimal value.

If you want precise float output, try this:

>>>float(10)/3
>>>3.3333333333333335

Any of the value should be float.

Nitin Verma
  • 206
  • 2
  • 14
0

The thing that you are doing wrong here is, you are making it believe that operands of division are integers( during division) .

I am sure you want to take two integers. You can do following things:

  1. use float() during division. In here you do not need to convert both Anzahl and spieler to float. Just convert spieler :

    q = anzahl/float(spieler)

**2. Use future module. then you do not need to change anything in your code. Write the following line at the beginning of your code :

from __future__ import division
  1. Use Python 3. In python 3 '/' means you will non-truncating division and '//' means truncating division
Ashish Kumar
  • 126
  • 11
0

If you divide an integer by an integer you will receive an integer. An integer is a whole number, no decimal points.

If you divide a float/integer by a float you will receive a float. A float is a whole number, including decimal points.

When you are doing your operations, change spieler to a float. That can be done with float(spieler).

Your code will look like this:

def spiel():
    anzahl=int(raw_input("Anzahl der Spieler"))
    float(spieler)=int(raw_input("Jeder wievielte spielt"))
    if anzahl>0 and spieler>0:
#Quotient berechnen
        q=anzahl/spieler
        print '%.2f'%q