【问题标题】:Issues with Calculator Program - Python计算器程序的问题 - Python
【发布时间】:2018-07-31 05:25:55
【问题描述】:

...我是 Python 新手,并且正在学习基础知识;目前我正在用 Python 处理这个计算器程序,但我遇到了以下项目的问题: 1.我想打印用户对所有用户输入的回答,当他们确定他们不再想要执行任何计算时..我将“calc_store”创建为一个空列表,但它还没有工作。 2. 如果用户在 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()

【问题讨论】:

    标签: python python-3.x calculator


    【解决方案1】:
    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=[]
    

    在这两个函数中,你需要它们是全局的或者你返回它们。我选择让它们全局化。

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

    2. 以 3 个不正确的选项结束,我猜您已经这样做了,但我做了一些更改以使其发挥作用。

    更多解释请在 cmets 中询问。

    【讨论】:

    • 使函数“全局”的使用是我一直在努力解决但没有正确实现的部分,所以我放弃了它,但这完全有道理。我过度考虑了列表的打印....感谢您的帮助和对改进的解释...感谢您的洞察力/帮助!
    猜你喜欢
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    相关资源
    最近更新 更多