【发布时间】: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