python - 代码不在 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
.
.
.
任何改进计算器代码的建议将不胜感激。谢谢!
解决方案
这是因为 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 open
sublime 配置文件是启用了 -option的自定义终结器配置文件。
推荐阅读
- arrays - 将 UserDefaults 移动到数组
- javascript - Uppy 刷新图像列表
- python - 如何在一个循环内同时创建多个循环?
- include - #include 检测到错误。请更新您的 includePath。此翻译单元已禁用 Squiggles (C:\\Users\\Desktop\\c c++\\disrealnew.c
- python-3.x - 是否可以使用来自 post 请求的 python (Django) 中的 zipFile 将任何文件写入 zip 文件。得到错误
- python - 无法在电子商务应用程序-django 中获取产品 ID
- java - 将自定义字段添加到 Hibernate Envers 修订表 (revinfo),如 operation_id。哪个是操作实体的PK
- html - 如何在列表中为 ::marker 添加 CSS 背景?
- sql-server - PyODBC + MS SQL Server + OPENROWSET 读取/加入 Excel 表与数据库表不通过 PyODBC 工作
- java - Windows 上的 URL 编码问题