Python 等价于 System('PAUSE')
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22466398/
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
Python Equivalent to System('PAUSE')
提问by MaxxB
I have been coding a basic calculator in python 3.3 and I want to be able to run it in command window.
我一直在用 python 3.3 编写一个基本的计算器,我希望能够在命令窗口中运行它。
but as soon as i get to the end it shuts the windows before I have time to view the final answer.
但是一旦我结束,它就会在我有时间查看最终答案之前关闭窗户。
so I was wondering if there is an equivalent to the c++ System('PAUSE') command to tell it not to go any further until the user is ready.
所以我想知道是否有一个等效于 c++ System('PAUSE') 命令的命令来告诉它在用户准备好之前不要再继续了。
Here is my calculator code:
这是我的计算器代码:
print('Your Very Own Basic Calculator')
first_num = int(input('Please Enter The First Number: '))
second_num = int(input('Please Enter The Second Number: '))
Equation = input('What would you like to do, multiplication, division, subtraction or
if Equation == ('*'):
addition? *, /, -, +')
print('The Answer is',first_num * second_num)
elif Equation == ("/"):
print('The Answer is',first_num / second_num)
elif Equation == ('-'):
print('The Answer is',first_num - second_num)
elif Equation == ('+'):
print('The Answer is',first_num + second_num)
thanks
谢谢
采纳答案by Filip Malczak
Use input()on p3k or raw_input()on p2.7x - it will read anything from stdin, so it will wait until user is ready.
使用input()上P3K或raw_input()在p2.7x -它会从标准读什么,所以它会等待,直到用户准备。
回答by TIMBERings
import time
time.sleep(secs)
The other option is better, but this answers Your question too.
另一种选择更好,但这也回答了您的问题。
回答by S1lur
import os
...
os.system("pause")
this should do the job
这应该可以完成工作
回答by Jean-Christophe Gay
as of today this is working under win7 :
截至今天,这是在 win7 下工作的:
import os
(...)
os.system("PAUSE")
Watch out the caps in the snippet, pause is not PAUSE.
注意代码片段中的大写字母,暂停不是暂停。
回答by Ioannis E. Kommas
Here is a simple program to do math with, A. "input()" that basically wait's for the user to input a value and based on that value calculates the result, also u will see B. While True: meaning the program never stops (or at least the user inputs 2 than asked to exit) Finally some part is on Greek lang. (u will need to translate them)
这是一个简单的数学程序,A.“input()”基本上等待用户输入一个值并根据该值计算结果,你也会看到 B.虽然为真:意味着程序永远不会停止(或者至少用户输入 2 而不是被要求退出)最后一部分是希腊语。(你需要翻译它们)
import os
# Reads a Float number
def read(thesi):
try:
ask_me = f'Δ?σε μου τον {thesi} αριθμ?'
os.system(f"say '{ask_me}'")
number_a = float(input())
return number_a
except KeyboardInterrupt:
print('Ζητ?θηκε ?μεση ?ξοδο? απ? το πρ?γραμμα')
quit()
except ValueError:
print('Σφ?λμα:')
return read(thesi)
def continue_exit():
try:
accept_only = ('1', '2')
ask_me = '1. Continue 2. Exit the Program'
os.system(f"say '{ask_me}'")
number= input()
if number in accept_only:
return number
else:
print('Try again')
return continue_exit()
except KeyboardInterrupt:
print('Ζητ?θηκε ?μεση ?ξοδο? απ? το πρ?γραμμα')
quit()
except ValueError:
print('Σφ?λμα:')
return continue_exit()
# Reads the type of the equation
def equation_read():
lst = ['+', '-', '/', '*', 'mod', 'pow', 'div']
ask_me = f'μαθηματικ? πρ?ξη?'
os.system(f"say '{ask_me}'")
print('Input --> + - / * mod pow div ')
equation = input()
if equation in lst:
return equation
print('Σφ?λμα')
return equation_read()
# Checks and does the math
def math(a ,b ,eq):
forbiden_zero_division = ('/', 'mode', 'div')
if b == 0 and eq in forbiden_zero_division:
ask_me = f'Σφ?λμα: Δεν επιτρ?πεται δια?ρεση με το 0'
os.system(f"say '{ask_me}'")
return 'Division by 0!'
if eq == '+':
ask_me = f'Ζητ?θηκε πρ?σθεση:'
os.system(f"say '{ask_me}'")
return a + b
elif eq == '-':
ask_me = f'Ζητ?θηκε αφα?ρεση:'
os.system(f"say '{ask_me}'")
return a - b
elif eq == '/':
ask_me = f'Ζητ?θηκε δια?ρεση:'
os.system(f"say '{ask_me}'")
return a / b
elif eq == '*':
ask_me = f'Ζητ?θηκε πολλαπλασιασμ??:'
os.system(f"say '{ask_me}'")
return a * b
elif eq == 'mod':
ask_me = f'Ζητ?θηκε το υπ?λοιπο δι?ιρεση?:'
os.system(f"say '{ask_me}'")
return a % b
elif eq == 'pow':
ask_me = f'Ζητ?θηκε η δ?ναμη:'
os.system(f"say '{ask_me}'")
return a ** b
elif eq == 'div':
ask_me = f'Ζητ?θηκε ακ?ραιη δια?ρεση:'
os.system(f"say '{ask_me}'")
return a // b
while True:
a = read('πρ?το') # Read the First Number
b = read('δε?τερο') # Read the Second Number
eq = equation_read() # Read the Type of the equation
result = math(a, b, eq) # Do the math
print(result)
ask_me = f'Αποτ?λεσμα {result}'
os.system(f"say '{ask_me}'")
c_e = continue_exit()
if c_e == '2':
break

