趁胜追击,于是完成作业以后,又申请了day3作业。用vscode打开python文件,写一个简单四则运算的计算器,能支持用户输入。
文章最后是今天写的代码。
遇到的问题:
- 把简单问题复杂化。想着什么计算界面,一个复杂的计算表达式如何在函数中表达,这些想法能出现,原因就在于没有仔细阅读作业要求。
使用python编写一个能加、减、乘、除的计算器,支持输入参数,支持输出结果。
以上就是作业要求,对于我这样有间歇性阅读略过的患者,只需要一字一字得去看。
- 使用vscode之前没有仔细阅读官方档案,致使运行时,出现了问题,只是单纯地去找代码中的问题,因为对代码不自信,却忘记了去想有可能是运行方式的问题。看了官方说明,才知道要选择run in teminal,这样input就没有问题了。
收获总结:
- 学会了抄代码。不再谴责自己没用,心安理得地抄了三种代码,然后运行,改写代码,最后改写出了自己的代码。
- 敢于调试,以前总是怕出现问题,手心冒汗,后背冒汗,脑袋发麻,如今出现问题我就搜索问题是什么意思,然后冷静地去想办法解决。
- 学会去issues找问题答案。今天的除数不能为零这个条件我就忘了写进代码,在查看issues时,看到了,省去了发表以后要改错这一步骤。
>def calculate():
operation = input('''
please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division
''')
number_1 = float(input('Please enter the first number: '))
number_2 = float(input('Please enter the second number: '))
if operation == '+':
print('{} + {} = {} '.format(number_1, number_2,number_1 + number_2))
elif operation == '-':
print('{} - {} = {} '.format(number_1,number_2,number_1 - number_2))
elif operation == '*':
print('{} * {} = {} '.format(number_1, number_2,number_1 * number_2))
elif operation == '/' and number_2 != 0:
print('{} / {} = {} '.format(number_1, number_2,number_1 / number_2))
else:
print('You have not typed a valid operator, please run the program again.')
calculate()
网友评论