计算器程序问题 - Python
Issues with Calculator Program - Python
...我是 Python 的新手,正在学习基础知识;目前我正在 Python 完成这个计算器程序,但我遇到以下问题:
1. 当用户确定他们不想再执行任何计算时,我想打印用户对所有用户输入的答案。我将 "calc_store" 创建为一个空列表,但它还没有工作。
2. 如果用户在 3 次尝试后无法输入其中一个已识别的数学运算,我想 "end" 或终止程序。我一直在第 71 行收到关于 "Indentation Error" 的错误陈述,但我不明白为什么。
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()
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=[]
在这两个函数中,您需要它们是全局的,或者您 return 它们。我选择让它们全球化。
以列表的形式保存计算。并且这个列表已经将操作转换为字符串,因为您只需要显示它们即可。
以 3 个不正确的选项结束,我想您已经这样做了,但我做了一些更改以使其生效。
更多解释请在评论中询问。
...我是 Python 的新手,正在学习基础知识;目前我正在 Python 完成这个计算器程序,但我遇到以下问题: 1. 当用户确定他们不想再执行任何计算时,我想打印用户对所有用户输入的答案。我将 "calc_store" 创建为一个空列表,但它还没有工作。 2. 如果用户在 3 次尝试后无法输入其中一个已识别的数学运算,我想 "end" 或终止程序。我一直在第 71 行收到关于 "Indentation Error" 的错误陈述,但我不明白为什么。 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()
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=[]
在这两个函数中,您需要它们是全局的,或者您 return 它们。我选择让它们全球化。
以列表的形式保存计算。并且这个列表已经将操作转换为字符串,因为您只需要显示它们即可。
以 3 个不正确的选项结束,我想您已经这样做了,但我做了一些更改以使其生效。
更多解释请在评论中询问。