Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

scope of int function in python (I am new to python )

I am learning python and this is the code that I have written , it is working fine but I am confused why I only have to change "age" into integer and not months , weeks or date. If I simply add "age = 25" then it does not give any error .

age = input("What is your current age? ")

Years_remaining = Years_remaining = (90 - int(age))
months = Years_remaining * 12
weeks =  Years_remaining * 52


days =  Years_remaining * 365

print (f"you have {days} days, {weeks} weeks, and {months} months left")

why I only have to convert age in INT when I am expecting user to enter the value and not when I am statically entering the value , I hope my question is making sense .

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is why:
age is str as this is what the input method returns, therefore, you have to cast to int to subtract it to 90 and store it in Years_remaining.

At this point, Years_remaining is an int, so months does not need any cast as both of its operands are now int (Years_remaining and 12).

If for example, you would cast age to float, then months, weeks, etc would be a float as well.

Does this make sense to you?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading