Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calculator
#1
problem is that when in sub() you give a letter instead of a number it will give a error and after that if you give both numbers it wont return anything Huh

import math

print("Calculator")

def sub():
    try:
        num1 = int(input("Give a number: "))
        num2 = int(input("Give a number: "))
    except (TypeError, ValueError):
        print("This input is invalid.")
        sub()
    else:
        return num1,num2
        
    
    
    

def main():
    while True:
        num1,num2 = sub()
        print("(1)+")
        print("(2)-")
        print("(3)*")
        print("(4)/")
        print("(5)sin(number1/number2)")
        print("(6)cos(number1/number2)")
        print("(7)Change numbers")
        print("(8)Quit")
        print("Current numbers: ", num1, num2)
        num3 = int(input("Please select something (1-6): "))
        if num3 == 1:
            print("The result is:", int(num1) + int(num2))
        elif num3 == 2:
            print("The result is:", int(num1) - int(num2))
        elif num3 == 3:
            print("The result is:", int(num1) * int(num2))
        elif num3 == 4:
            print("The result is:", int(num1) / int(num2))
        elif num3 == 5:
            print("The result is:", (math.sin(int(num1) / int(num2))))
        elif num3 == 6:
            print("The result is:", (math.cos(int(num1) / int(num2))))
        elif num3 == 7:
            num1 = int(input("Give the first number: "))
            num2 = int(input("Give the second number: "))
            continue
        elif num3 == 8:
            print("Thank you!")
            break
        else:
            print("Selection was not correct.")

        
if __name__ == "__main__":
    main()
Reply
#2
as in your main() function, use a while statement

def sub():
    while True:
        try:
            num1 = int(input("Give a number: "))
            num2 = int(input("Give a number: "))
        except (TypeError, ValueError):
            print("This input is invalid.")
            # sub()
            continue
        else:
            return num1, num2
You should also account for division by zero (selections 4, 5 and 6).  You do not wish to use decimal numbers?
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
#3
i changed sub() to num_1() and num_2()

it works fine untill i use a letter. then i get this error

    print("The result is:", int(num1) - int(num2))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
>>>

import math

print("Calculator")

def num_1():
    try:
        num1 = int(input("Give a number: "))
    except (TypeError, ValueError):        
        print("This input is invalid.")
        num1 = None
        num_1()
    else:
        return num1

def num_2():
    try:
        num2 = int(input("Give a number: "))
    except (TypeError, ValueError):
        print("This input is invalid.")
        num2 = None
        num_2()
    else:
        return num2
        
    
    
    

def main():
    while True:
        num1 = num_1()
        num2 = num_2()
        print("(1)+")
        print("(2)-")
        print("(3)*")
        print("(4)/")
        print("(5)sin(number1/number2)")
        print("(6)cos(number1/number2)")
        print("(7)Change numbers")
        print("(8)Quit")
        print("Current numbers: ", num1, num2)
        num3 = int(input("Please select something (1-6): "))
        if num3 == 1:
            print("The result is:", int(num1) + int(num2))
        elif num3 == 2:
            print("The result is:", int(num1) - int(num2))
        elif num3 == 3:
            print("The result is:", int(num1) * int(num2))
        elif num3 == 4:
            print("The result is:", int(num1) / int(num2))
        elif num3 == 5:
            print("The result is:", (math.sin(int(num1) / int(num2))))
        elif num3 == 6:
            print("The result is:", (math.cos(int(num1) / int(num2))))
        elif num3 == 7:
            num1 = None
            num2 = None
            main()
        elif num3 == 8:
            print("Thank you!")
            break
        else:
            print("Selection was not correct.")

        
if __name__ == "__main__":
    main()
Reply
#4
Your original post works fine, if you add the "while" in the sub() function. See my reply above.  Also remember to comment out or better yet, delete the "sub()" within the "sub()" function as it is not needed, again, see my reply above.
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
#5
yeah but i need them to be asked separately
Reply
#6
First, you're doing the same thing twice. You have two functions that are basically identical. Make one function that gets a number, and use that function twice.

Second, follow the code. What happens when you have an invalid input? It goes into the except block, and calls itself again recursively. What does it do with the result of calling itself? Nothing. You need to return the result of the function calling itself. (return num_1())
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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