0

So I'm a fresh beginner when it comes to coding and here is my first calculator. The "add" function works but instead of actually adding the numbers it just adds the inputs together. For example, if the inputs are 5 and 6 instead of printing 11 it will print out 56. That is my first problem. Second is that I get flashed with this error which is shown at the bottom of the code. Anyone can explain what I'm doing wrong and how to fix it for the future reference?

print("Welcome to my simple calculator")

while True:
    print("These are your options")
    print("Enter \"add\" to multiply two number together")
    print("Enter \"multiply\" to multiply two number together")
    print("Enter \"divide\" to multiply two number together")
    print("Enter \"subtract\" to multiply two number together")
    print("If you are done just type \"quit\"")
    user_input = input(":")

if user_input == "quit":
    break

elif user_input == "add":
    num1 = int(input("Enter the first number :"))
    num2 = int(input("Enter the second number"))
    result = str(int(num1)*int(num2)
    print(f'Your answer: {result}')

elif user_input == "multiply":
    num3 = input("Enter the first number :")
    num4 = input("Enter the second number")
    result = str(num3*num4)
    print("Your answer "+result)

elif user_input == "divide":
    num5 = input("Enter the first number :")
    num6 = input("Enter the second number")
    result = str(num5*num6)
    print("Your answer "+result)

elif input == "subtract":
    num7 = input("Enter the first number :")
    num8 = input("Enter the second number")
    result = str(num7-num8)
    print("Your answer "+result)

I know this is basic. But for a noobie is hard. If you want to post a fixed version of the code please do, I would be very grateful for an explanation of how you got there and what was wrong with my code.

Thanks in advance!

smttsp
  • 4,011
  • 3
  • 33
  • 62
Slappy
  • 11
  • 3
  • 1
    The result of `input()` is always a string, but you want to perform your operations on integers or floats. – jordanm Apr 21 '20 at 14:59

3 Answers3

2

The result from an input() statement will always be a string unless specified differently. The reason you are getting that error is because you are trying to multiply two strings together. so either when you get the user to input the numbers use: int(input()) or you can do the sum like this.

result = int(num)*int(num1)

and if you wanted the answer of the sum to be a saved as a string (looking at your code that doesn't seem the case):

result = str(int(num)*int(num1))

Let me know if that works for you.

andypaling1
  • 142
  • 12
  • HI, thank you very much for your answer, So when i change the code to thave int(input()) it flashes with with a error saying TypeError: can only concatenate str (not "int") to str – Slappy Apr 21 '20 at 15:35
  • Ok, can you add your new code to the question again, so I can see better. – andypaling1 Apr 21 '20 at 15:42
  • However, I can take a guess that it is because when you print out ```print('Your answer:' + result) ``` you are getting the error. This would be because you are trying to add a string with an int at the end. You could re write it like this: ```print('Your answer: ', result)``` or print(f'Your answer: {result}'). There are other ways of getting around this: https://stackoverflow.com/questions/51252580/getting-a-typeerror-can-only-concatenate-str-not-int-to-str – andypaling1 Apr 21 '20 at 15:46
  • elif user_input == "add": num1 = int(input("Enter the first number :")) num2 = int(input("Enter the second number")) result = str(int(num1)*int(num2) print("Your answer " : {result}) This is the code and its throwing the same error – Slappy Apr 21 '20 at 15:51
  • ```print(f'Your answer: {result}') ``` – andypaling1 Apr 21 '20 at 15:53
  • Is there anywhere we can contact other than here? Its still saying that Print in an invalid syntax – Slappy Apr 21 '20 at 15:54
  • Not sure if its allowed on here, edit your main question with the whole new code so i can copy and paste it into my IDE so I can see the issue properly. Should take a few mins – andypaling1 Apr 21 '20 at 15:56
  • I updated it, have a go – Slappy Apr 21 '20 at 15:57
  • Ok, ill upload a new answer with all the code. – andypaling1 Apr 21 '20 at 16:02
  • Please stop answering the same question all the time. These are called duplicates and they should be flagged for closure rather than answered. – Andras Deak -- Слава Україні Apr 22 '20 at 23:14
0

Here is the completion of your code. Try this:

print("Welcome to my simple calculator")

while True:
    print("These are your options")
    print("Enter \"add\" to multiply two number together")
    print("Enter \"multiply\" to multiply two number together")
    print("Enter \"divide\" to multiply two number together")
    print("Enter \"subtract\" to multiply two number together")
    print("If you are done just type \"quit\"")
    user_input = input(":")"

    if user_input == "quit":
        break

    elif user_input == "add":
        num1 = int(input("Enter the first number :"))
        num2 = int(input("Enter the second number"))
        result = int(num1 + num2)
        print(result)

    elif user_input == "multiply":
        num3 = input("Enter the first number :")
        num4 = input("Enter the second number")
        result = int(num3 * num4)
        print(result)

    elif user_input == "divide":
        num5 = input("Enter the first number :")
        num6 = input("Enter the second number")
        result = int(num5 / num6)
        print(result)

    elif user_input == "subtract":
        num7 = input("Enter the first number :")
        num8 = input("Enter the second number")
        result = int(num7 - num8)
        print(result)
nk07
  • 161
  • 3
  • 12
-1

New:

print("Welcome to my simple calculator")

while True:
    print("These are your options")
    print("Enter \"add\" to multiply two number together")
    print("Enter \"multiply\" to multiply two number together")
    print("Enter \"divide\" to multiply two number together")
    print("Enter \"subtract\" to multiply two number together")
    print("If you are done just type \"quit\"")
    user_input = input(":")

    if user_input == "quit":
        break

    elif user_input == "add":
        num1 = int(input("Enter the first number :"))
        num2 = int(input("Enter the second number"))
        result = num1+num2
        print('Your answer:', result)

    elif user_input == "multiply":
        num3 = int(input("Enter the first number :"))
        num4 = int(input("Enter the second number"))
        result = num3*num4
        print("Your answer ", result)

    elif user_input == "divide":
        num5 = int(input("Enter the first number :"))
        num6 = int(input("Enter the second number"))
        result = str(num5/num6)
        print("Your answer ", result)

    elif input == "subtract":
        num7 = int(input("Enter the first number :"))
        num8 = int(input("Enter the second number"))
        result = num7-num8
        print("Your answer ", result)

Here you go :) let me know if its good

andypaling1
  • 142
  • 12
  • It works perfect, can you quickly explain to me what you fixed? So i know recognise my mistake in the future. Thanks – Slappy Apr 21 '20 at 16:12
  • Just a few things, mostly with data types, ie, if you do ```int(input())``` there is no need to covert to int when multiplying the two nums together. Aside from that, I think there was a basic syntax error from spelling but overall nothing major. – andypaling1 Apr 21 '20 at 16:17
  • @andypaling1 Above code will definetly throws error when you try "Subtract" – nk07 Apr 21 '20 at 19:12
  • @nk07 Just tested it for me with subtract and ive had no issue. – andypaling1 Apr 21 '20 at 19:37
  • I have fixed the code with subtract, took me a good minute ahah but its all down and working. Thanks guys ! – Slappy Apr 21 '20 at 19:53
  • @Slappy , what was the issue? – andypaling1 Apr 21 '20 at 19:54