Jump to content

Help with converting a String to a integer. PYTHON

This is for my brothers birthday and here is my code so far 

name = input("enter Name ")
input("Is " + name + " correct? ")
month = input("enter month that you were born in ")
input("Is " + month + " correct? ")
day = input("enter day that you were born in ")
input("Is " + day + " correct? ")
year = input("enter year that you were born in ")
input("Is " + year + " correct?")
age = 2023 - year
print("seems like It is" + name + age + "BIRTHDAY!!!!!!!!!!!" )

 

Link to comment
Share on other sites

Link to post
Share on other sites

what error are you getting. cant you not just leave it in python?

Link to comment
Share on other sites

Link to post
Share on other sites

it says "unsupported operand type(s) for -: 'int' and 'str'"

Link to comment
Share on other sites

Link to post
Share on other sites

The problematic line is this:

 

age = 2023 - year

and I think the error message would have given you the line number where the error occured.

 

In this case, input() returns a string, and you are trying to subtract a string (year) from a integer literal (2023).

 

One way to alleviate this is to cast year into an integer and then cast age into a string.

 

age = 2023 - int(year)
print("seems like It is" + name + str(age) + "BIRTHDAY!!!!!!!!!!!" )

 

Link to comment
Share on other sites

Link to post
Share on other sites

30 minutes ago, nateplayz50 said:

Ok I will try this

just a tip you have to quote us otherwise we dont get pinged and mostlikly will not reply

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, nateplayz50 said:

It works

do you know datatypes ? I think you need to learn that first

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, nateplayz50 said:

It works

python has built in data types for manipulating data

 

integers (int): whole numbers without a decimal point

floating points (float): numbers with a decimal point

strings (str): this is what the built in input() returns (even if the user inputs a number). are written inside single or double quotes

boolean (bool): True or False, used for control statements

 

and tuples, sets, dictionaries

 

You can learn more about these data types from:

https://www.geeksforgeeks.org/python-data-types/

https://www.w3schools.com/python/python_datatypes.asp

 

 

 

hey! i know to use a computer

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

×