我已经编写了一个简单的代码来运行calculator。它仍处于开发阶段,尚未完成。我看到代码没有在运行python 3.6.4的sublime文本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
崇高文字3的输出
Welcome to calculator
Pick an option from the below,
1.Add 2.Subtract 3.Divide 4.Multiply 5.Quit
1
10
78@
23516
.
.
.
任何改进计算器代码的建议将不胜感激。谢谢!
这是因为崇高的输出面板只是捕获并显示构建系统的输出。它不侦听输入:如果在代码中包含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",
}
此构建系统将当前文件$file
中的python脚本发送给终结器,终结器使用其sublime配置文件执行该脚本。崇高配置文件是启用了When command exists - Hold terminal open
-选项的自定义终止配置文件。