python中的简单加法程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16881955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple addition program in python
提问by Eka
I am trying to learn python and for that purpose i made a simple addition program using python 2.7.3
我正在尝试学习 python,为此我使用 python 2.7.3 制作了一个简单的加法程序
print("Enter two Numbers\n")
a = int(raw_input('A='))
b = int(raw_input('B='))
c=a+b
print ('C= %s' %c)
i saved the file as add.pyand when i double click and run it;the program run and exits instantenously without showing answer.
我将文件保存为add.py,当我双击并运行它时;程序运行并立即退出而不显示答案。
Then i tried code of this question Simple addition calculator in pythonit accepts user inputs but after entering both numbers the python exits with out showing answer.
然后我尝试了这个问题的代码简单的加法计算器在 python它接受用户输入,但在输入两个数字后,python 退出而不显示答案。
Any suggestions for the above code. Advance thanks for the help
对上述代码的任何建议。提前感谢您的帮助
采纳答案by jamylak
add an empty raw_input()at the end to pause until you press Enter
raw_input()在末尾添加一个空以暂停,直到您按下Enter
print("Enter two Numbers\n")
a = int(raw_input('A='))
b = int(raw_input('B='))
c=a+b
print ('C= %s' %c)
raw_input() # waits for you to press enter
Alternatively run it from IDLE, command line, or whichever editor you use.
或者从IDLE、命令行或您使用的任何编辑器运行它。
回答by Mike Müller
Run your file from the command line. This way you can see exceptions.
从命令行运行您的文件。这样你就可以看到异常。
Execute cmdthan in the "dos box" type:
执行cmd比在“dos box”类型中:
python myfile.py
Or on Windows likley just:
或者在 Windows 上 likeley 只是:
myfile.py
回答by Mr_Spock
It's exiting because you're not telling the interpreter to pause at any moment after printing the results. The program itself works. I recommend running it directly in the terminal/command line window like so:
它正在退出,因为您没有告诉解释器在打印结果后随时暂停。该程序本身有效。我建议直接在终端/命令行窗口中运行它,如下所示:


Alternatively, you could write:
或者,你可以写:
import time
print("Enter two Numbers\n")
a = int(raw_input('A='))
b = int(raw_input('B='))
c=a+b
print ('C= %s' %c)
time.sleep(3.0) #pause for 3 seconds
Or you can just add another raw_input()at the end of your code so that it waits for input (at which point the user will type something and nothing will happen to their input data).
或者您可以raw_input()在代码的末尾添加另一个,以便它等待输入(此时用户将输入一些内容并且他们的输入数据不会发生任何变化)。

