Candidate: Ezra Assessed by: Nicholas Tollervey
Python (2023) ~ Grade 1 (Initial)
A simple calculator
This application uses simple functions to do basic arithmetic(addition, subtraction, multiplication and division) based on the selections of the user. This project is built for people who do simple mathematical calculations. It only works with two numbers. when the app is launched (i.e python <filename>), the user is presented with a couple of choices to make based on what he wants to do with application. When the choice is made, the app will the ask for the numbers involved and do the math for you.
Status: Submitted for assessment.
Ezra ~ 23 Nov 2022 4:22 a.m. (updated: 28 Nov 2022 12:57 a.m.)
operator = input(f""" select operation from below
1. Add
2. Subtract
3. Multiply
4. Divide
: """)
# check the operator value and perform the operation
if operator == 'Add' or operator == 'add' or operator == '+':
num1 = float(input("Enter first number: "))
num2 = int(input("Enter second number: "))
add = num1 + num2
print(add)
elif operator == 'Subtract' or operator == "subtract" or operator == '-':
num1 = float(input("Enter first number: "))
num2 = int(input("Enter second number: "))
minus = num1 - num2
print(minus)
elif operator == 'Multiply' or operator == 'multiply' or operator == '*':
num1 = float(input("Enter first number: "))
num2 = int(input("Enter second number: "))
product = num1 * num2
print(product)
elif operator == 'Divide' or operator == 'divide' or operator == '/':
num1 = float(input("Enter first number: "))
num2 = int(input("Enter second number: "))
div = num1 / num2
print(div)
else:
print("Invalid input")
Nicholas Tollervey ~ 23 Nov 2022 3:13 p.m.
Hi Ezra,
Thank you so much for submitting your code and I'm looking forward to working with you to improve your project.
Before we continue there are some things you need to either change or add - so I have all the things I need to be able to assess your work and help you grow as a developer.
Please can you make the following changes and additions:
- The code you have added to the project isn't formatted correctly. Please read our guidance about using Markdown, then update your entry to correct the formatting. Markdown is used by professional coders to format technical documentation, which is why it's important to know how it works! Put simply, you need to update the entry so your code is between three back-ticks (
```
) like this:
```python
<your code goes here>
```
- In case it's not clear, we need more than just your code. 😉 We're not just assessing your project, but also the process and work you put in to make the project. We want to see your thought processes, design decisions and the way you addressed, overcame or worked around inevitable problems. Your final mark isn't JUST about the quality of the final project, but is also based upon how you engaged with the creation, design and development of it..! 👍
- The best thing you can do is to add more comments to this project that cover all the things described in our guide to project essentials. Put simply, please make sure:
- You've told us what your project does, who it's for and why you made it (you've almost covered all of this in the project description, so you're very close).
- You've added comments to the project that record or describe your technical decisions, design choices and explain any problems you faced. Put simply, please describe how you created your project. Tell us a good story..!
- You've explained how to use your project. Just imagine you're writing a user guide (I realise using a calculator might be obvious to you and me, but it might not be clear to everyone). The important aspect of this is, your project has some sort of documentation.
- I do hope this makes sense, and if not please don't hesitate to ask questions as new comments added to this project. Asking questions is a good thing because it is evidence that you're engaging with the coding problem and is itself one of the things we look for during assessment. 😊
Once these things are updated, we'll be in a good place to review, refine and revise your project into a version that reflects your current level of coding, and which I can use as the basis of my final assessment.
Many thanks in advance..!
Nicholas Tollervey ~ 24 Nov 2022 2:33 p.m.
Hi Ezra,
I also just wanted to let you know that adding my first comment to your project highlighted a couple of bugs in the CodeGrades website. I've since fixed them, but I wanted to say thank you..! I'm looking forward to your revised version of the project. I'll get alerts when you add comments here so I'll be able to see you make the requested changes. 👍
Nicholas Tollervey ~ 04 Dec 2022 11:34 a.m. (updated: 04 Dec 2022 11:41 a.m.)
Dear Ezra,
Thank you for updating your code. It looks so much better.
I've just run your project and it works! 👍 I even tried to use it in not-very-friendly ways, and it recovered well 🤣 :
$ python calc.py
select operation from below
1. Add
2. Subtract
3. Multiply
4. Divide
: 5
Invalid input
$ python calc.py
select operation from below
1. Add
2. Subtract
3. Multiply
4. Divide
: sausages
Invalid input
But I also got some unexpected behaviour.
For example, the opening "menu" of operations looks like a list, and I wanted to add two numbers, so typed 1
(since that was the item number, for addition):
$ python calc.py
select operation from below
1. Add
2. Subtract
3. Multiply
4. Divide
: 1
Invalid input
I had to look at the code to work out why it wasn't a valid input. If I were not a technical user, I'm not sure I'd understand why 1
was invalid input. How might you make this easier for a non-technical user?
...and then I found this bug. The project crashed with an error:
$ python calc.py
select operation from below
1. Add
2. Subtract
3. Multiply
4. Divide
: add
Enter first number: 1
Enter second number: 2.5
Traceback (most recent call last):
File "/home/ntoll/src/scratch/codegrades/ezra/calc.py", line 12, in <module>
num2 = int(input("Enter second number: "))
ValueError: invalid literal for int() with base 10: '2.5'
Can you investigate this, explain what went wrong, and fix it please..? 👍
I also accidentally hit return twice, while entering the numbers, and the project crashed. Could these two bugs be related..?
$ python calc.py
select operation from below
1. Add
2. Subtract
3. Multiply
4. Divide
: add
Enter first number: 3
Enter second number:
Traceback (most recent call last):
File "/home/ntoll/src/scratch/codegrades/ezra/calc.py", line 12, in <module>
num2 = int(input("Enter second number: "))
ValueError: invalid literal for int() with base 10: ''
I have a feature request too... currently I can only operate on two numbers. What if I want to add, say, a whole list of numbers...? How might you do that..? (And if this isn't clear, have a look around the Internet, investigate possible solutions and just ask... remember I'm here to help, and asking questions or pointing out where and why you're stuck on a problem is a good thing - since it shows evidence of engaging with and growing through a problem 📈. The only wrong question, is the question you don't ask. 😄 )
I have some comments about the code:
- What is the
f
before the"""
in the first line for..? Do you even need it..? - The structure of your project is a long conditional (
if ... elif ... else...
). Inside each block of the conditional, you repeat much of the same code. For example, you ask fornum1
andnum2
multiple times as well asprint
the answer many times. If you wanted to change this behaviour, you'd have to change each one of these blocks - which is a bit of a pain. How might you update your code so you only ever ask fornum1
andnum2
once, and only everprint
the outcome once..? - The conditions check the user's input matches an operation. For example, you do:
operator == 'Add' or operator == 'add' or operator == '+'
to match for an "addition" operation. What if I told you that Python has a shortcut for this..? For example, if I wanted to check if an item was in my shopping list I might use:if item in ["eggs", "bread", "milk", ]:
instead ofif item == "eggs" or item == "bread" or item == "milk":
. Can you update your code to use this pattern? - I may want to do multiple calculations, and have an option to "quit" your project when I had finished. How might I do this (hint: investigate
while
loops).
Please don't be disheartened by the level of detail or amount of feedback I'm giving... this is to be expected and reflects the good start you've made to the project. Of course, CodeGrades is all about learning with a mentor, and so my suggestions, questions and requests for updates are part of this process. 👍
One final (and very important) thing. You have submitted your project for grade 3. The project, as it currently stands, is at around a grade 1 level. I think, after we collaborate on the project, it could be a strong grade 2 level. But to get this project to a grade 3 level would take a significant amount of work.
I'm conscientious that I don't want you to find yourself in a situation where the wonderful work you've been doing is being measured against too high a standard of grading. To put this into perspective... when I was at PyCon Ghana 🇬🇭 I learned to play Oware and managed to buy a board. Clearly, it would be rather disheartening to practice playing with an Oware grand-master (too high a level)... so far better to play against folks at the right level for me. Similarly, I think it would be much better for you if we adjusted the grade to grade 2 (I can do this, as an admin user of CodeGrades).
What do you think..? Do let me know..!
The most important thing is the CodeGrades process helps and supports you in your learning journey to experience, rather than makes you feel disheartened.
Of course, once you have worked towards your grade 2, then grade 3 will become much easier to do (rather than starting from grade 3). And since you're an alpha tester of CodeGrades, all your grading will be free anyway..! 😉
I've had a lot of fun trying our your project, especially when I was trying to break it in interesting ways (it's very resilient code!).
In summary, you have several things to do:
- Fix the bugs. 🐛
- Address the features I've asked for. 👍
- Think about and update the code to reflect my review. 🤔
- Let me know about revising this to a grade 2 rather than grade 3 project. 🏆
I'm looking forward to seeing how you get on, and (one final reminder) I'm here to help, so just ask questions via this project (such interactions are important evidence for the assessment and will show others how you positively engage with coding). 🚀
Best of luck..! 👍
Nicholas Tollervey ~ 04 Dec 2022 11:49 a.m.
One more thing... when you've revised your code, add it here as a new comment. That way it becomes clear how your project has evolved and improved. 🚀
Ezra ~ 27 Dec 2022 1:08 a.m. (updated: 27 Dec 2022 1:10 a.m.)
operator = input(f""" select operation from below
1. Add
2. Subtract
3. Multiply
4. Divide
: """)
num1 = float(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# check the operator value and perform the operation
if operator in [ 'Add', 'add', '+','1']:
add = num1 + num2
print(add)
elif operator in ['Subtract','subtract','2' ,'-']:
minus = num1 - num2
print(minus)
elif operator in ['Multiply','multiply','*','3']:
product = num1 * num2
print(product)
elif operator in ['Divide','divide','/','4']:
div = num1 / num2
print(div)
else:
print("Invalid input")
please am still working on how to work with more than two numbers. I am done with the addition but am facing challenges with the other operators.
operator = input(f""" select operation from below
1. Add
2. Subtract
3. Multiply
4. Divide
: """)
numbers = input("Enter numbers: ")
data = list(map(float, numbers.split()))
# check the operator value and perform the operation
if operator in [ 'Add', 'add', '+','1']:
diff = data[0]
for i in range(1,len(data)):
diff = diff + data[i]
print(diff)
elif operator in ['Subtract','subtract','2' ,'-']:
diff = data[0]
for i in range(1,len(data)):
diff = diff - data[i]
print(diff)
elif operator in ['Multiply','multiply','*','3']:
diff = data[0]
for i in range(1,len(data)):
diff = diff * data[i]
print(diff)
elif operator in ['Divide','divide','/','4']:
diff = data[0]
for i in range(1,len(data)):
diff = diff / data[i]
print(diff)
else:
print("Invalid input")
Nicholas Tollervey ~ 03 Jan 2023 4:14 p.m.
Ezra,
Happy new year 🎉, and thank you for your updates. The project is definitely moving forward, and please keep up the good work.
First of all, I've changed the level for your grade to grade 1, since this is more closely aligned to the code you've written and the feedback you'll get as a result will be more useful for you. The URL for your grade is now: https://www.codegrades.com/user/Ezra/python2023/grade1/
I have read your code and have some updates for you to think about:
- Do you need the "f" in
f"""
in theinput
line? Can you tell me what thef
does (or doesn't do)? - When I try to use the latest version of your code I did this:
select operation from below
1. Add
2. Subtract
3. Multiply
4. Divide
: 1
Enter numbers: 1, 2, 3, 4
Traceback (most recent call last):
File "/home/ntoll/mu_code/ezra.py", line 10, in <module>
data = list(map(float, numbers.split()))
ValueError: could not convert string to float: '1,'
I realise the problem is of my own making, but when you ask the user to enter number could you perhaps explain what form they should take (for example, Enter numbers (separated by a space):
)
- Clearly I am an idiot, because I also did this (can you spot the problem?):
select operation from below
1. Add
2. Subtract
3. Multiply
4. Divide
: ADD
Enter numbers: 1 2 3
Invalid input
I think you'd find this article about the lower
method helpful in coming up with a solution... especially the bit at the end.
- I completely understand why you're doing
for i in range(1, len(data)):
. But what if I told you, it's possible to simply get each number without having to usedata[i]
..? For example, how might you change your code given the following example of what I mean?
result = 0
for number in data:
result = result + number
I hope you agree that for number in data
is much easier to read and understand than for i in range(1, len(data))
. It's also more idiomatic to write it this way (the other way, being the classic C-ish way to do things, which is why I understand why you're doing it!)
- I also think you could use something called assignment operators in Python. Click on the link and especially pay attention to the parts about
+=
,-=
,*=
and/=
.
As always Ezra, I'm here to help, so please don't hesitate to ask questions or seek guidance. It's yet more evidence that you're pro-actively engaging in the grading process, so don't hold back. I think, if you address the points I've just raised, I'll be in a position to assess your project and give you your mark and feedback. 🚀