Python中相当于System('PAUSE')的写法
我在用Python 3.3编写一个简单的计算器,我想在命令窗口里运行它。
但是一到最后,窗口就关掉了,我还没来得及看到最终的答案。
所以我在想,是否有类似于C++中的System('PAUSE')命令的东西,可以让程序在用户准备好之前不继续往下执行。
这是我的计算器代码:
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)
谢谢
5 个回答
0
这里有一个简单的程序可以用来做数学运算,
A. “input()”这个函数基本上是让用户输入一个值,然后根据这个值来计算结果,你会看到
B. “While True:” 这段意思是程序会一直运行下去(或者至少用户输入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
4
import os
...
os.system("pause")
这段代码应该能解决问题。
8
import time
time.sleep(secs)
另一个选项更好,但这个也回答了你的问题。
11
截至今天,这段代码在Windows 7上可以正常运行:
import os
(...)
os.system("PAUSE")
注意代码中的大小写,"pause" 不是 "PAUSE"。
21
在Python 3(p3k)中使用 input()
,而在Python 2.7(p2.7x)中使用 raw_input()
。这两个函数会从标准输入(stdin)读取数据,也就是说,它们会一直等着,直到用户准备好输入内容。