Python中等效于System('PAUSE')的命令是什么?

9

我一直在用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)

thanks

5个回答

21

在p3k上使用input()或在p2.7x上使用raw_input() - 它会从stdin读取任何内容,因此会等待用户准备好。


我要做的就是添加一个While循环,这样当它到达结尾时,它会询问您是否想进行另一个求和,这样您就可以在决定是否输入另一个求和时看到它。我之前应该想到了,我的错 :/ 但还是谢谢@FilipMalczak - MaxxB

11

截至今日,此项工作可在Win7下运行:

import os
(...)
os.system("PAUSE")

请注意代码片段中的大写字母,pause和PAUSE是不同的。


此函数与 @S1lur 的函数非常相似,但仅适用于Windows系统,Linux/Unix发行版将抛出错误。 - nash

7
import time

time.sleep(secs)

另一个选项更好,但这也回答了您的问题。

4
import os
...
os.system("pause")

这应该可以完成工作。


6
sh: 1: pause: not found翻译:sh: 1: pause: 找不到命令 - leovp

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接