How can I get the variable to subtract the input please?

This is the place for queries that don't fit in any of the other categories.

How can I get the variable to subtract the input please?

Postby Alkydale » Mon Nov 18, 2013 11:54 pm

This will be very simple to most of you I guess but it's killing me!

Code: Select all
print ("Please type in your age")
age =  input ()
leave = 16
print ("You have" + leave - age + "years left at school")

I want to have an input where the users age is inserted and then subtracted from the variable age which is set to 16 and the answer displayed as You have x years left at school.

Help much appreciated.

Thank you
Last edited by Mekire on Mon Nov 18, 2013 11:57 pm, edited 1 time in total.
Reason: First post lock. Code tags. Please read "New Users Read This" thread.
Alkydale
 
Posts: 4
Joined: Mon Nov 18, 2013 11:49 pm

Re: How can I get the variable to subtract the input please?

Postby Mekire » Tue Nov 19, 2013 12:00 am

Welcome to the forum. Be sure to read through this.

In general we require you to, along with posting your code, tell us what result you expected, and what error or result you are recieving. Also please let us know if you are using python 2 or python 3.

In this case your problem is you are trying to do math with incompatible types (string and int) and trying to concatenate them with strings without converting them or properly formatting your print line.

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: How can I get the variable to subtract the input please?

Postby Alkydale » Tue Nov 19, 2013 12:04 am

Thank you.

I'm getting this error

Code: Select all
Please type in your age
Traceback (most recent call last):
  File "C:\Python33\school years.py", line 4, in <module>
    print ('You have ' + leave_school_at - your_age + 'years left at school')
TypeError: Can't convert 'int' object to str implicitly
>>>
Last edited by Mekire on Tue Nov 19, 2013 12:13 am, edited 2 times in total.
Reason: Tracebacks in code tags as well please.
Alkydale
 
Posts: 4
Joined: Mon Nov 18, 2013 11:49 pm

Re: How can I get the variable to subtract the input please?

Postby Mekire » Tue Nov 19, 2013 12:07 am

Indeed. input() returns a string (even if you type in a number). You need to convert it to the type you want (probably int) before you can do math with it. You will instantly hit another glitch if you do this correctly, but take a shot.

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: How can I get the variable to subtract the input please?

Postby Alkydale » Tue Nov 19, 2013 12:08 am

Mekire wrote:Indeed. input() returns a string (even if you type in a number). You need to convert it to the type you want (probably int) before you can do math with it. You will instantly hit another glitch if you do this correctly, but take a shot.

-Mek


Cheers, I'll have another go, could be a long night.......................
Alkydale
 
Posts: 4
Joined: Mon Nov 18, 2013 11:49 pm

Re: How can I get the variable to subtract the input please?

Postby Mekire » Tue Nov 19, 2013 12:12 am

Of particular intersest to you will be the builtin functions int() and str().

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: How can I get the variable to subtract the input please?

Postby Alkydale » Tue Nov 19, 2013 11:18 am

Thanks for your help.

I used this to make it work:

Code: Select all
print ("Please type in your age")
age = input ()
age = int(age)
leave_at = 16
print ("You have",leave_at - age, "years left at school")
Last edited by Mekire on Tue Nov 19, 2013 11:34 am, edited 1 time in total.
Reason: Lock.
Alkydale
 
Posts: 4
Joined: Mon Nov 18, 2013 11:49 pm

Re: How can I get the variable to subtract the input please?

Postby Mekire » Tue Nov 19, 2013 11:34 am

Please use code tags when posting code.

Good work on your problem. I would make some minor suggestions:
Code: Select all
age = input("Please type in your age: ")
age = int(age)
leave_at = 16
years_left = leave_at-age
print("You have {} years left at school".format(years_left))

Also consider the fact that your program will crash if the user doesn't behave as you expect.

For example:
Code: Select all
>>>
Please type in your age: ten
Traceback (most recent call last):
  File "C:\Users\Owner\Desktop\temp.py", line 2, in <module>
    age = int(age)
ValueError: invalid literal for int() with base 10: 'ten'
>>>

You can bulletproof against this if you are so inclined; your instructor may or may not expect it (probably not at this point).

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests