python计算器程序与预期的缩进块错误

时间:2014-10-11 12:46:43

标签: python

我已经创建了一个python计算器,我需要让它重新启动,我已经添加了循环:

#This line defines the end of the program so it restarts.
def sys():


#These lines will define each operation.
def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def Multiply(x, y):
    return x * y

def Divide(x, y):
    return x/y
#This asks the user what operation they would like to use
print("Please select operation ")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

#This tells the user to enter the number of the operation they would like
operation = input("Enter operation(1/2/3/4):")

#This asks the user to input the two numbers they would like the calculator to calculate
num1 = int(input("Please enter first number: "))
num2 = int(input("Please enter second number: "))


#This is the part of the program that will calculate the calculations
if operation == '1':
    print(num1, "+", num2, "=", add(num1,num2))

elif operation == '2':
   print(num1,"-",num2,"=", subtract(num1,num2))

elif operation == '3':
   print(num1,"*",num2,"=", subtract(num1,num2))

elif operation == '4':
   print(num1,"/",num2,"=", subtract(num1,num2))
else:
    print("Invalid input")

inp = input("Enter clear to play again or exit to exit")
if inp == "clear":
    sys()
else:
    print("thanks for playing")
    sys.exit()

它一直说expected an indented block并表示它想要在前面的缩进:

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def Multiply(x, y):
    return x * y

def Divide(x, y):
    return x/y 

但是当我添加它们时,它一直说,操作没有定义。我也觉得循环也不起作用。

3 个答案:

答案 0 :(得分:1)

看起来你试图以递归方式重新运行代码,在这种情况下你可能想要这样的代码。

import sys # so you can use sys.exit()

def add(x, y): # no need for these function definitions to be in the loop
    ...

...

def main(): # conventional name - sys shadows the module you just imported
    print("Please select operation ")
    print("1. Add")

    ...

    inp = input("Enter clear to play again or exit to exit")
    if inp == "clear":
        main()
    else:
        print("Thanks for playing")
        sys.exit() # or just 'return'

if __name__ == "__main__": # if run directly, rather than imported
    main()

您可以在其他功能中定义功能(虽然这里没有必要),但请记住您需要另一级别的缩进:

def outer(n):
    def inner(x):
        return x ** 2
    return 2 * inner(n)

请注意,使用递归意味着您最终会达到系统递归深度限制;迭代可能更明智:

def main():
    while True:
        ...
        inp = input("Enter clear to play again or exit to exit")
        if inp != "clear":
            print("Thanks for playing")
            break

答案 1 :(得分:0)

您可能对此问题的解决方案感兴趣。它使用operator模块,该模块为所有标准Python运算符提供命名函数。它还使用列表将操作号转换为函数和打印符号。

from operator import add, sub, mul, div

operations = [
    (add, '+'),
    (sub, '-'),
    (mul, 'x'),
    (div, '/'),
]

while True:

    print('1. Add')
    print('2. Subtract')
    print('3. Multiply')
    print('4. Divide')

    op = int(raw_input('Enter operation (1/2/3/4): '))

    if 0 < op <= len(operations):
        func, sym = operations[op-1]
        num1 = float(raw_input('Please enter first number: '))
        num2 = float(raw_input('Please enter second number: '))
        print('{} {} {} = {}'.format(num1, sym, num2, func(num1, num2)))
    else:
        print('Invalid operation')
        continue

    while True:
        inp = raw_input('Enter clear to play again or exit to exit: ')
        if inp == 'exit':
            print('Thanks for playing')
            exit()
        elif inp == 'clear':
            break

<强>输出

1. Add
2. Subtract
3. Multiply
4. Divide
Enter operation (1/2/3/4): 3
Please enter first number: 8
Please enter second number: 9
8.0 x 9.0 = 72.0
Enter clear to play again or exit to exit: exit
Thanks for playing

答案 2 :(得分:-1)

如果您有一个空函数,请输入短语pass,如下所示:

def some_func():
    pass

这个错误将不再发生。