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

E

Ed Taylor

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

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
 
A

Amit Saha

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

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.

I assume you are using Python 3. In that case, the input() function
always returns a string:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'int'

And hence you cannot perform a subtraction operation. You will have to
convert the input into a data type such as an integer or a float and
then try to do any mathematical operation:
0.0

Hope that helps.

Best,
Amit.
 
A

Andrew Berg

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

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
You provided a short code snippet to help describe your problem, which is good, but you omitted what happened and what you expected. You
also didn't mention which version of Python you're using (I am going to assume some 3.x; if you are using 2.x, I *strongly* recommend
switching to 3.x unless you have a very compelling reason not to). If a traceback is printed, try to make sense of it, and if you can't,
post it in its entirety and ask us to explain it.
From what I can tell, you are trying to mix integers with strings. Python is strongly typed; it won't try to guess what you want when you
try to do things that make no sense for a given type. input() returns a string and you have defined leave as an integer. Using the +
operator to concatenate makes sense for strings, but it makes sense to do addition when dealing with number types like integers and floats.
If you try to do something silly like add a number to a string, Python will complain because it doesn't know what to do (and again, it won't
try to guess). With that in mind, rewrite the code so that you do number operations on numbers and string operations on strings.
Hint: there are builtin functions to cast between types.
Hint 2: it's a good idea to split up operations to separate lines so that it's easy to see which operation has a problem if there is one.
 
A

alex23

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

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.

Hey there,

When asking code questions, if you get a traceback it's often handy to
include it so we can see exactly what problem you've hit.

Luckily, it's pretty obvious here:

1. input() binds a string to 'age', whereas 'leave' is an integer; you
cannot subtract a string from an integer, you need to turn the string
into an integer first. Try:

age = int(input())

2. With this done, you still have a similar issue: 'leave - age'
produces an integer, and you cannot concatenate strings & integers. You
can use string formatting to take care of this:

print("You have {} years left at school".format(leave - age))

There's a lot more to formatting than this, make sure to check out the
docs for it:

http://docs.python.org/3.1/library/string.html#format-string-syntax

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top