Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Error
#1
So i started making a really simple calculator. im just a begginer it was a good training for me.

print("Hello there, enter your first number")
num1 = input()
print("Enter your second number")
num2 = input()
print("What Do you want to do with these numbers? Please enter Divide/Add/Remove/Duplicate")
thing = input()
if thing == "Duplicate":
        print(int(num1 * num2))
if thing == "Remove":
        print(int(num1 - num2))
if thing == "Divide":
        print(int(num1 / num2))
if thing == "Add":
        print(int(num1 + num2))
When i use the add command i am getting the two numbers combined. lets say i did 4 and 4 so im getting 44
and the rest of the commands simply show me this error

Traceback (most recent call last):
File "C:/Users/Yonatan/PycharmProjects/Python Programs/Calculator.py", line 8, in <module>
print(int(num1 * num2))
TypeError: can't multiply sequence by non-int of type 'str'


Thank you very much for helping! Smile
Reply
#2
you have your integer conversion at the wrong place.
it should be on the input.
print(int(num1 + num2))
says add string num1 to string num2 and then convert bazaar result to integer
Reply
#3
You are trying to multiply to strings together, num1 and num2. You need them to be numeric prior to you calculation, in your case, since you're trying to make a calculator is to make them 'floats':

num1 = float(input())
In fact, save yourself some time and paper with:

num1 = float(input("Hello there, enter your first number"))
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Forum Jump:

User Panel Messages

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