Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Maths problems
#1
Hi
I wrote a simple program and it doesn't work properly. Instead of adding numbers it creates a number out of it, for example I type 1+2 and it answers 12. Here's the code:
a = input("Hey! My name's Python. I can sum two numbers. Enter the first number.\n")
b = input("Now enter the second number.\n")
number = a + b
print("Here you are! Your number is %s" % number)
Thanks for support.
Nick
Reply
#2
The input() function returns string values rather than numbers. When you use the + operator on two strings, you are concatenating them rather than adding them. You need to convert your variables a and b to int or float before you add them.
Reply
#3
Interesting math. 1 + 2 = 12. 123 + 456 = 123456

If I had this problem the first thing I would do is verify that math works correctly in Python.
print(1 + 2)
print(123 + 456)
Output:
3 579
Hmmm. It looks like Python knows how to add, so what else could be the problem? The next thing I would do is verify I was entering numbers.
a = input("Enter Number ")
print('type(a) =', type(a))
Output:
Enter Number 1 type(a) = <class 'str'>
Even though I typed a number Python says I entered a string. Guess I need to find out how "input" works. I could look this up in the documentation, but you can also ask for help from the Python terminal.
help(input)
Output:
input(prompt=None, /) Read a string from standard input. The trailing newline is stripped. The prompt string, if given, is printed to standard output without a trailing newline before reading input. If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError. On *nix systems, readline is used if available.
Sure enough, Python returns a string from standard input. Actually in older versions of Python (pre 3.0), input worked differently. The old input tried to evaluate the text you typed, converting it to floats or ints if you typed a number. To read a string you had to raw_input.

To add two numbers you will have to convert the strings returned by "input" into numbers.
a = int(input("Enter Number "))
b = int(input("Enter Number "))
number = a + b
print(a, '+', b, '=', a+b)
Output:
Enter Number 1 Enter Number 2 1 + 2 = 3
Output:
Enter Number 1 Enter Number 2.3 Traceback (most recent call last): File "...junk.py", line 2, in <module> b = int(input("Enter Number ")) ValueError: invalid literal for int() with base 10: '2.3'
int() does not work if the string is not a valid representation of an integer. Though 2.3 is a number, it is not an integer.
a = float(input("Enter Number "))
b = float(input("Enter Number "))
number = a + b
print(a, '+', b, '=', a+b)
Output:
Enter Number 1 Enter Number 2.3 1.0 + 2.3 = 3.3
Output:
Enter Number one Traceback (most recent call last): File "C:\Users\djhys\Documents\python\musings\junk.py", line 1, in <module> a = float(input("Enter Number ")) ValueError: could not convert string to float: 'one'
The program works for floats and ints, but it throws an exception for anything that is not a float or int. This may be acceptable, but maybe you would like a simpler error message.
try:
    a = float(input("Enter Number "))
    b = float(input("Enter Number "))
    number = a + b
    print(a, '+', b, '=', a+b)
except ValueError as errmsg:
    print(errmsg)
Output:
Enter Number 1 Enter Number two could not convert string to float: 'two'
Even simple programs are surprisingly complex.
Reply
#4
(Sep-26-2020, 12:07 PM)Nick_oLay Wrote: I wrote a simple program and it doesn't work properly.

Your program works properly, it just don't return result you expect.

This is called polymorphism i.e. the ability of different types of objects to respond to the same message differently.

Some examples:

>>> 4 + 2
6
>>> '4' + '2'
'42'
>>> [4] + [2]
[4, 2]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python for maths help flo5564 1 1,745 Feb-18-2019, 06:37 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020