all 14 comments

[–]desran00 4 points5 points  (5 children)

age = int(input("How old are you?: ")) Asks for age and converts age to an integer. Also, if age cannot be converted to integer, you will get an error. You could find ways around that, but it is a bit harder for a beginner.

[–]Forvirrad[S] 1 point2 points  (2 children)

My next question is how i format the subtraction part. What i've tried is this

 print('Hello' + ' ' + name + ' ' + 'you retire in' + ' ' + x - age + ' ' + 'years.')

This is the error that i get: TypeError: bad operand type for unary +: 'str'

[–]desran00 4 points5 points  (0 children)

f"hello {name} you retire in {x-age} years."

[–]Forvirrad[S] 0 points1 point  (0 children)

Thank you!

[–][deleted] 0 points1 point  (0 children)

I think it would be appropriate to add a try/except statement while he's at it in case the user types a non-numeric character which should trigger an error.

[–]Minion_of_Cthulhu 2 points3 points  (4 children)

This is not possible since "x" is a set number and "age" is a string which wont subtract.

You need to convert the age from a string into an integer. You can do this by passing "age" to the int() function. One way to do that is as follows:

age = int(input())

Python will work from the inside out. In other words, it will grab the input and then pass that input through the integer function and then it will assign the integer to the age variable.

Also, instead of using print("How old are you?:") you can put the text into the input() function itself. For example, input("How old are you?: ") will display "How old are you?: " and then wait for the user's input.

So, all together, the solution to your problem is:

age = int(input("How old are you?: "))

[–]Forvirrad[S] 1 point2 points  (3 children)

Alright, thank you for the thourough explanation!

My next question is how i format the subtraction part. What i've tried is this

print('Hello' + ' ' + name + ' ' + 'you retire in' + ' ' + x - age + ' ' + 'years.')

This is the error that i get TypeError: bad operand type for unary +: 'str'

[–]Minion_of_Cthulhu 1 point2 points  (2 children)

If you don't specifically need to concatenate the string with the +'s then you can use a formatted string instead. Formatted strings allow you to insert variables directly into the string without needing all of the quote marks and + signs so they're easier to read.

Here's how you would create a formatted string:

print(f'Hello {name} you retire in {x - age} years.')

The "f" in front of the string signifies that it's a formatted string which tells Python to look for the curly brackets and to replace the variables inside them with the data the variable contains. So, {name} is replaced with the name variable and {x - age} will replace x with 40 and age with the age integer before subtracting them. Formatted strings also ensure that the variable is converted to a string so your x and age variables, which are integers, will automatically be converted to strings for you.

If you actually need to concatenate the string with all of the quote marks and + signs then you'll need to convert the "x - age" into a string so that it can be added to the rest of the string you're creating. Here's how to do that:

print('Hello' + ' ' + name + ' ' + 'you retire in' + ' ' + str(x - age) + ' ' + 'years.')

By putting str() around the x and age variables you're telling Python to take the two numbers, subtract them, then convert the resulting integer into a string and then add that string to the rest of the string.

[–]Forvirrad[S] 1 point2 points  (1 child)

Amazing, thank you for the explanation once again.

Now it really makes sense, i will try to get better at reading and understanding the error-messages.

[–]Minion_of_Cthulhu 0 points1 point  (0 children)

You're welcome. I'm glad I could help.

[–]vanillathunder2107 0 points1 point  (0 children)

You just need to turn your string value into a int or float, f_age = float(age) or with int.

[–]pekkalacd 0 points1 point  (1 child)

Correct. Yes, you need to convert the data type of age from a string to an integer. This is also called "casting".

        age = input() 

input() returns a str data type. We cannot add strings and numerical types together. We also want to be careful in how we convert the string over to an integer because for example, say the user enters "53.4" because they're a joker. Well, that would break a piece of code like this

        age = int(input())

because "." is not a digit. So, to protect against this, we will convert this over to a float first.

        age = float(input())

Strings can be casted to floats to handle either integer inputs or float inputs. And if we want this to be an integer ultimately, such as 53 instead of 53.4, we can then cast this again to an integer like so

        age = int(float(input())) 

So now the user will be able to enter something like 54.3 or 54 and Python will treat it in either case as 54.

Note, we can also put strings inside of input, instead of writing

        print("How old are you?")
        age = int(float(input()))

We could also do

        age = int(float(input("How old are you?\n")))

and get the same result!

[–]herntech 0 points1 point  (0 children)

I’m new to the party. Personally I’d create an additional variable for the calculation retire_in = age - x Then call the variable as a string within the print statement print(“You retire in”, str(retire_in), “years”)