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

2013-11-18 Thread Amit Saha
On Tue, Nov 19, 2013 at 9:56 AM, Ed Taylor  wrote:
> 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:

>>> age = input()
10
>>> age
'10'
>>> age - 10
Traceback (most recent call last):
  File "", line 1, in 
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:

>>> int(age) - 10
0
>>> float(age)-10
0.0

Hope that helps.

Best,
Amit.


-- 
http://echorand.me
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2013-11-18 Thread alex23

On 19/11/2013 9:56 AM, Ed Taylor wrote:

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.

--
https://mail.python.org/mailman/listinfo/python-list


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

2013-11-18 Thread Andrew Berg
On 2013.11.18 17:56, Ed Taylor wrote:
> 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.

-- 
CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 10.0
-- 
https://mail.python.org/mailman/listinfo/python-list