Jump to content

In python how to you have this happen

image.png.2474dcee0e3944e8025d5471210bcbc2.pngMy brothers birthday is tomorrow this so this is urgent. 

Link to comment
Share on other sites

Link to post
Share on other sites

You need to convert the string from input to an int first before you can subtract it from a int.

Link to comment
Share on other sites

Link to post
Share on other sites

The problem is that your program thinks that "year" is a word (string, or str for short), not a number (integer, int for short).

 

You can't take a word and subtract 2023 from it. That's why you get the error

TypeError: unsupported operand type(s) for -: 'int' and 'str'

 

We can solve this by converting your string to an integer. Python includes a function called int() that can do this. What you do is call the function and pass it the string you want to be converted.

I want you to try and fix this issue using the int() function and come back with an updated script.

Please remember that you need to save the converted variable as well.

 

 

This feels like homework so I hope nobody posts the right answer for you. Even if it is for your brother's birthday I feel like you figuring it out by yourself makes the experience far more rewarding and special.

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, LAwLz said:

The problem is that your program thinks that "year" is a word (string, or str for short), not a number (integer, int for short).

 

You can't take a word and subtract 2023 from it. That's why you get the error

TypeError: unsupported operand type(s) for -: 'int' and 'str'

 

We can solve this by converting your string to an integer. Python includes a function called int() that can do this. What you do is call the function and pass it the string you want to be converted.

I want you to try and fix this issue using the int() function and come back with an updated script.

Please remember that you need to save the converted variable as well.

 

 

This feels like homework so I hope nobody posts the right answer for you. Even if it is for your brother's birthday I feel like you figuring it out by yourself makes the experience far more rewarding and special.

I have a hour left and i am panicking.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, nateplayz50 said:

I have a hour left and i am panicking.

As I said, you need to convert from a string to an integer. The function int() does this in Python. You pass the variable you want to be converted into an integer into the function, and then it spits out a converted variable for you.

 

If I had a variable called money and wanted it converted from a string to an integer, I would type this:

money = int(money)

 

int takes whatever is inside the (), and converts it into an integer. The "money = int()" part catches the output and saves it into the variable "money". 

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, nateplayz50 said:

I have a hour left and i am panicking.

Bit late, but what people are saying is you need to convert the string to int.

 

So

Quote

age = 2023 - int(year)

Admittedly small code projects like this ChatGPT can also really help in speed of development 😉

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×