首页 > 解决方案 > 代码不在 sublime 3 上运行,但在在线编译器上运行

问题描述

我编写了一个简单的代码来运行计算器
它仍处于开发阶段,尚未完成。
我看到代码没有在运行 python 3.6.4 的 sublime text 3 上运行。
事实上,代码陷入了无限循环。但在线编译器并非如此。

源代码

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


在线编译器的输出(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


Sublime 文本 3 上的输出

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


任何改进计算器代码的建议将不胜感激。谢谢!

标签: pythonpython-3.xsublimetext3

解决方案


这是因为 sublime 中的输出面板只是捕获和显示构建系统的输出。它不侦听输入:如果您input在代码中包含一条语句,您的 python 解释器将永远不会收到您提供的输入,因此您的程序将挂起。

为了克服这个问题,您可以安装在 SublimeREPL 包中并通过 python REPL 执行您的代码。或者,也许更方便的是,您可以创建自己的 sublime 构建系统,它在外部 shell 中执行您的 python 代码。在我的 Linux 系统上,我为终端使用终结器,并且我有一个如下所示的 python 构建系统:

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

该构建系统将当前文件中的 python 脚本发送$file到 terminator,后者使用其 sublime 配置文件执行它。When command exists - Hold terminal opensublime 配置文件是启用了 -option的自定义终结器配置文件。


推荐阅读