Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

I can's seem to get this simple calculator to work. Can anyone assist? Thank you!

# My Calculator Script: version 1.0




def first():
    global first_num
    first_num = (input("Please enter the first number:\n"))
    int(first_num)
    if first_num == 0:
        print("Error, please choose a value higher than 0.\n")
        first()

def operators():

    global enter_op
    enter_op = input("Please enter an operator. You can choose: -, +, /, or *.\n")
    str(enter_op)
    if enter_op not in ("-", "+", "/", "*"):
        print("Error, please enter a valid operator. You can choose from -, +, /, or *\n")
        operators()

def second():

    global second_num
    second_num = input("Please enter the second number:\n")
    int(second_num)
    if second_num == 0:
        print("Error, please choose a value higher than 0.\n")
        second()

def solution():


    if enter_op == ("+"):
        print(first_num + second_num)
    elif enter_op == ("-"):
        print(first_num - second_num)
    elif enter_op == ("/"):
        print(first_num / second_num)
    elif enter_op == ("*"):
        print(first_num * second_num)
    else:
        print("What happened!")


first()
operators()
second()
solution()

1 Answer

Hi Jason

    first_num = (input("Please enter the first number:\n"))
    int(first_num)

Calling the int() function on the variable first_num wont change the value in first_num to be of the integer type. You have to re-assign the variable with the changed value.

    first_num = (input("Please enter the first number:\n"))
    first_num = int(first_num)

Thanks!