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

Time:12-03

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 .

CodePudding user response:

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?

  • Related