0

I've written a simple code to run calculator.
It's still in development stage, not yet completed.
I see that the code is not running on sublime text 3 where am running python 3.6.4.
In fact, the code is falling in an infinite loop. But this is not the case in online compilers.

Source code

while True:
    print("Welcome to calculator")
    print("Pick an option from the below,")
    print("1.Add 2.Subtract 3.Divide 4.Multiply 5.Quit")
    user_input = int(input())

    if(user_input == 1):
        print("!!!Addition of numbers!!!")
        print("Provide atleast 2 numbers to add : ")
        numbers_arr = []
        while True:
            numbers = float(input())
            numbers_arr.append(numbers)
            if(len(numbers_arr) >= 2):
                print("1.Add 2.insert more numbers")
                option = int(input())
                if(option == 1):
                    result = sum(numbers_arr)
                    print("Addition of numbers is : {}".format(result))
                    break
                elif(option == 2):
                    numbers = float(input())
                    numbers_arr.append(numbers)
                else:
                    print("Invalid option idiot!")
                    numbers_arr=[]
                    break
            else:
                continue

    if(user_input == 2):
        print("!!!Subtraction of numbers!!!")
        numbers_arr = []
        while True:
            numbers = float(input())
            numbers_arr.append(numbers)
            if(len(numbers_arr) >= 2):
                print("1.Subtract 2.insert more numbers")
                option = int(input())
                if(option == 1):
                    result = 0
                    for i in numbers_arr[::-1]:
                      result = i - result
                    print("Subtraction of numbers is : {}".format(result))
                    break
                elif(option == 2):
                    numbers = float(input())
                    numbers_arr.append(numbers)
                else:
                    print("Invalid option idiot!")
                    break
            else:
                continue                

    if(user_input==5):
        break


Output on Online compiler (repl.it,onlinegdb)

Welcome to calculator
Pick an option from the below,
1.Add 2.Subtract 3.Divide 4.Multiply 5.Quit
1
!!!Addition of numbers!!!
Provide atleast 2 numbers to add : 
12
2
1.Add 2.insert more numbers
1
Addition of numbers is : 14.0
Welcome to calculator
Pick an option from the below,
1.Add 2.Subtract 3.Divide 4.Multiply 5.Quit


Output on Sublime text 3

Welcome to calculator
Pick an option from the below,
1.Add 2.Subtract 3.Divide 4.Multiply 5.Quit
1
10
78@
23516
.
.
.


Any suggestions in improving the code for calculator will be appreciated. Thank you!

1 Answer 1

1

This is because the output panel in sublime is just capturing and displaying the output of the build system. It does not listen for input: if you include an input statement in your code, your python interpreter will never receive the input you provide and your program will thus hang.

To overcome this problem, you can install in the SublimeREPL package and execute your code through the python REPL. Alternatively, and maybe more conveniently, you could create a sublime build system of your own, which executes your python code in an external shell. On my Linux system, I'm using terminator for my terminals, and I have a python build system that looks like this:

{
    "shell_cmd": "terminator -p sublime -e \"python -u $file; echo [Finished with exit code \\$?]\"",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
}

This build system sends the python script in the current file $file to terminator, which executes it using its sublime profile. The sublime profile is a custom terminator profile with the When command exists - Hold terminal open-option enabled.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks asparc. I didn't quite understand everything what u said. Except that we need to change the terminal to run this program. So i am running it on cmd, and it works well.
And do you start cmd separately, or from within sublime? My post was mainly about the latter :-)
I used to start within sublime but that didn't work for above code, so i ran with cmd and it worked fine.
Alright. As an alternative, you can make your own sublime build system that automatically starts a cmd and automatically runs the code in that cmd. That's what I suggested in my post, but for terminator, instead of cmd.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.