计算器程序的问题 - Python

时间:2018-02-20 18:46:11

标签: python python-3.x calculator

...我是Python&的新手。通过基础工作;目前我正在使用Python中的计算器程序,但我遇到以下问题: 1.当我们确定他们不再需要执行任何计算时,我想打印用户对所有用户输入的答案。我创建了" calc_store"作为一个空列表,但它还没有工作。 我想"结束"如果用户在3次尝试后无法输入所识别的数学运算之一,则终止程序。我一直收到有关"缩进错误"的错误陈述。在第71行,但我无法弄清楚原因。 3.我是否使用适当的方法将用户输入保存到列表中?

答案很棒,但解释很重要,所以我可以学习!代码如下:

def Calculations():  
    Calc_store=[]
    answer_count = 0
    incorrect_count = 0

    acknow = input("Please choose from the following math operations: +,-,*,//, **:  ")
    num1 = int(input("Please enter your first number: "))

    if acknow == '**':

        pow1 = int(input("Please enter your first number*: "))
        pow2 = int(input("To what power?"))

        print('{} ** {} = '.format(pow1, pow2))
        print(pow1 ** pow2)
        Power_1= pow2 ** pow2
        Calc_store.append(Power_1)
        answer_count +=1
        Repeat_Calculations()          


    elif acknow == '+':
        num2 = int(input("Please enter your second number: "))
        print('{} + {} = '.format(num1, num2))
        print(num1 + num2)
        Addition_1= num1 + num2
        Calc_store.append(Addition_1)
        answer_count +=1
        Repeat_Calculations()

    elif acknow == '-':
        num2 = int(input("Please enter your second number: "))
        print('{} - {} = '.format(num1, num2))
        print(num1 - num2)
        Subtract_1= num1 - num2
        Calc_store.append(Subtract_1)
        answer_count +=1
        Repeat_Calculations()

    elif acknow == '*':
        num2 = int(input("Please enter your second number: "))
        print('{} * {} = '.format(num1, num2))
        print(num1 * num2)
        Multiply_1= num1 * num2
        Calc_store.append(Multiply_1)
        answer_count +=1
        Repeat_Calculations()

    elif acknow == '/':
        num2 = int(input("Please enter your second number: "))
        print('{} / {} = '.format(num1, num2))
        print(num1 / num2)
        Divide_1= num1 / num2
        Calc_store.append(Divide_1)
        answer_count +=1
        Repeat_Calculations()


    else:
        print("Sorry I don't recognize that, try another operatror")
        incorrect_count +=1

    if incorrect_count > 2:
        print("Too many incorrect answers")


    Repeat_Calculations()

def Repeat_Calculations():

    calculate2 = input("Do you want to calculate again? Please type Y for YES or N for NO: ")

    if calculate2.upper() == 'Y':
        Calculations()
    elif calculate2.upper() == 'N':
        # This was the piece that I couldn't get to work...can you please tell me what I'm doing wrong?
        print('Bonsoir Elliot! The result of your %s calculculations were: [%s]' %(answer_count, Calc_store))
    else:
        Repeat_Calculations()



Calculations()

1 个答案:

答案 0 :(得分:0)

incorrect_count = 0
answer_count = 0
Calc_store=[]
def Calculations():  
    global incorrect_count
    global answer_count
    global Calc_store
    operations = ['+','-','*','//', '**']
    acknow = input("Please choose from the following math operations: +,-,*,//, **:  ")

    if acknow == '**':

        pow1 = int(input("Please enter your first number*: "))
        pow2 = int(input("To what power?"))

        print('{} ** {} = '.format(pow1, pow2))
        print(pow1 ** pow2)
        Power_1= pow2 ** pow2

        Calc_store.append(str(pow1)+' ** '+str(pow2)+" = "+str(Power_1))
        answer_count +=1
        Repeat_Calculations()          


    elif acknow == '+':
        num1 = int(input("Please enter your first number: "))
        num2 = int(input("Please enter your second number: "))
        print('{} + {} = '.format(num1, num2))
        print(num1 + num2)
        Addition_1= num1 + num2
        Calc_store.append(str(num1)+' + '+str(num2)+" = "+str(Addition_1))
        answer_count +=1
        Repeat_Calculations()

    elif acknow == '-':
        num1 = int(input("Please enter your first number: "))
        num2 = int(input("Please enter your second number: "))
        print('{} - {} = '.format(num1, num2))
        print(num1 - num2)
        Subtract_1= num1 - num2
        Calc_store.append(str(num1)+' - '+str(num2)+" = "+str(Subtract_1))
        answer_count +=1
        Repeat_Calculations()

    elif acknow == '*':
        num1 = int(input("Please enter your first number: "))
        num2 = int(input("Please enter your second number: "))
        print('{} * {} = '.format(num1, num2))
        print(num1 * num2)
        Multiply_1= num1 * num2
        Calc_store.append(str(num1)+' * '+str(num2)+" = "+str(Multiply_1))
        answer_count +=1
        Repeat_Calculations()

    elif acknow == '//':
        num1 = int(input("Please enter your first number: "))
        num2 = int(input("Please enter your second number: "))
        print('{} // {} = '.format(num1, num2))
        print(num1 // num2)
        Divide_1=num1 // num2
        Calc_store.append(str(num1)+' // '+str(num2)+" = "+str(Divide_1))
        Calc_store.append(Divide_1)
        answer_count +=1
        Repeat_Calculations()


    else:
        print("Sorry I don't recognize that, try another operatror")
        incorrect_count +=1
        if incorrect_count > 2:
            print("Too many incorrect answers")
            return -1
        Calculations()




    Repeat_Calculations()

def Repeat_Calculations():

    calculate2 = input("Do you want to calculate again? Please type Y for YES or N for NO: ")

    if calculate2.upper() == 'Y':
        Calculations()
    elif calculate2.upper() == 'N':
        # This was the piece that I couldn't get to work...can you please tell me what I'm doing wrong?
        print('Bonsoir Elliot! The result of your %s calculculations were:' %(answer_count))
        for cal in Calc_store:
            print(cal)
    else:
        Repeat_Calculations()


Calculations()

所以这是代码。我从您的代码中所做的更改: 1.因为你正在使用

incorrect_count = 0
answer_count = 0
Calc_store=[]

在这两个函数中,您需要它们是全局的或者返回它们。我选择让他们成为gloabl。

  1. 以列表的形式保存计算。并且此列表已将操作转换为字符串,因为您只需要显示它们。

  2. 结束了3个不正确的选项,我猜你已经做过了,但我做了一些改变才能让它发挥作用。

  3. 更多有关解释请在评论中提问。