计算器程序Python的问题

2024-09-30 20:17:23 发布

您现在位置:Python中文网/ 问答频道 /正文

…我是Python新手,正在学习基础知识;目前我正在用Python学习这个计算器程序,但我在以下方面遇到了问题: 1当用户确定不再需要执行任何计算时,我想打印所有用户输入的答案。我创建了一个空列表“calc\u store”,但它还没有工作。 2我想“结束”或终止程序,如果用户无法进入其中一个确定的数学运算后,3次尝试。我不断得到关于第71行“缩进错误”的错误陈述,但我不知道为什么。 三。我是否使用适当的方法将用户输入保存到列表中?你知道吗

答案是伟大的,但解释是重要的,所以我可以学习!代码如下:

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()

Tags: storeanswerinputyourcountcalcintrepeat
1条回答
网友
1楼 · 发布于 2024-09-30 20:17:23
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个错误的选项结束,我猜你已经做了,但我做了一些改变,使之工作。

更多解释请在评论中询问。你知道吗

相关问题 更多 >