My Python code works, but I don’t understand why an int()

Asked

Viewed 174 times

1

I did a time-calculation program on medications. According to the calculations everything is running perfectly, but I did not understand a detail, why the need to use a int() at a certain point in time.

Can you explain me? Follow the complete code and below the specific part of the 'problem'':

# utf -8
import shutil

columns = shutil.get_terminal_size().columns
print("Olá, Rafaela, vamos calcular quantos dias seus medicamentos irão durar!".center(columns))

quantidade1 = float((input("Quantos comprimidos de 300MG você possui? ")))
quantidade2 = float((input("Quantos comprimidos de 600MG você possui? ")))
dosagem = float((input("Quantos MG você toma diariamente? ")))

total_mg = (quantidade1 * 300) + (quantidade2 * 600)

dias = (total_mg / dosagem)
meses = int((dias / 30))

if dias < 30:
    print("Seus medicamentos irão durar %.2f dias!".center(columns) % dias)
elif 60 > dias >= 30:
    dias -= 30
    print("Seus medicamentos irão durar %d mês e %d dias!".center(columns) % (meses, dias))
else:
    dias = dias - (meses * 30)
    print("Seus medicamentos irão durar %d meses e %d dias!".center(columns) % (meses, dias))

The question is: if on line 14 I remove the int(), the else simply stops working properly and all answers will be X months and 0 days (always 0 days), but I did not understand why. Can someone explain to me?

meses = int((dias / 30))

Here is the example of output of code with and without int(), to the else, with the inputs() of which 240, 450 and 1500.

Seus medicamentos irão durar 7 meses e 18 dias! using int()

Seus medicamentos irão durar 7 meses e 0 dias! not using int()

  • Because you are casting float for integer. Your print is using %d to show the number, which expects an integer. Try switching from %d to %.2f as above and take out the int.

  • 1

    If you wrote the code, I strongly recommend reviewing your development process. The natural is first you understand the problem and from there elaborate the solution; not elaborate a solution to from it seek to understand the problem, as he did.

  • I switched to %.2f and took the int, but the problem came back! Your medications will last 822463 months and 0.00 days!

  • As for the development process, I understand the point, but it’s only because I was trying to put into practice what I learned about Control Flow even and it seemed all right, but exceptionally the Else did not match the expected result. I went through all the calculations thinking they were the cause, but in the end it turned out to be this int.

  • The function int() removes all values after ".". It serves to transform a value to decimal basis integer, which ends up truncating the values to 0 decimal places. So instead of being "3.5 months", the correct one to show to the user would be "3 months and 15 days"

1 answer

0


Your problem is in the last condition, more precisely in dias = dias - (meses * 30) (which you can simplify to dias -= (meses * 30)).

If you don’t force months to be a whole (using int()), dias -= (meses * 30) at all times shall be zero (since dias = meses * 30).

With the example you set, meses = 7.6 and meses = int(7.6) = 7. Do you notice the difference? It is always interesting to check the official documentation if you have doubts about the behaviors of built-in functions.

Still with your example, dias = 228.0. Soon, 228.0 -= 7.6*30 = 0 and 228.0 -= 7*30 = 18.

  • Damn, thank you very much! I broke my head a little bit but I understood, in the end I ended up making the mistake of logic by multiplying meses * 30 that nothing else was the opposite of what I had done before and the two ended up subtracting. I also realized that if I put dias -= (int(meses) * 30) only on this line, would also work. Thank you very much!

  • @Alecsandercamilo if the answer helped you, accept it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.