+ 1
How to make a calculator!? More info in the question body
I need to make a simple calulator that only allows the math problem number's to be under 50. I almost did it, but I failed at the part that prints the answer. PLEASE HELP!!!! Also, I made it on a Python IDE. So I need to make it work on a python IDE
15 Respuestas
+ 7
When we look at the output, we observe 2 things:
1. The output is a list.
2. The items in the list are strings.
To calculate, we need numbers. Hence, we need to convert the strings to numbers and check what the operator is so we can do the requested computation.
Additionally, you need to check for each number if it is in the range of valid numbers.
+ 6
Stanley Nguyen
You need to share your code, So that we can recognise the part of code where you get stuck and find its solution.
+ 5
Here are some additional thoughts:
* Do not use global lists – instead, return the value from the function. When you haven't learned about returning values, first find a solution without functions.
* Name your functions according to what they do – not according to when you have defined them. That is, name them "get_number()" instead of "one()".
* You are not using your "options" anywhere – neither for displaying them to the user nor for input validation. Remove or use them.
* Functions should help to make code reusable. Your functions "one" and "three" do the same thing, so one of them should be enough.
* Mind the gap: Write "one()" instead of "one ()" to keep the code clean and avoid mistakes.
+ 4
Please present your code attempt.
True if a<50 and b<50 else False
+ 4
Here is one possible approach:
1. Get inputs and store them in the variables num1, op, and num2. You can use print() for instance, to show a hint to the user what they should enter.
2. Convert num1 and num2 to a numeric format, e.g. using float().
Have you learned about try-except blocks yet?
3. Test for num1 and num2 if they are in the range of valid numbers.
You could output a message if the user entered an invalid number.
4. Use an if-elif-... chain to perform the computation according to the operator and store the result in a variable. Example:
if op == "+":
result = num1 + num2
....
5. Output the result.
6. When you have made it so far, we could try to perform some more input validation or write functions to "pack it up".
Sooner or later, someone will drop a cade snippet using "eval()". However, this isn't strictly a calculator.
+ 2
Good, the first part seems to work!
The conditions in lines 12, 22, 32 are the same. When choice == "No", we will always go down the 1. branch (division) and never reach + or -.
How about asking the user to enter "+", "-", "/", "*" and then check if choice == ...?
In line 25 and 34 it should be
if choice == "Yes" or choice == "yes"
(Note that we need choice for both comparisons individually.)
Or is it on purpose that the user should answer these yes-no questions? Instead of entering the numbers and sign directly, I mean?
Here's some pseudo code to demonstrate what I mean:
num1 = float_input
op = string_input
num2 = float_input
if op == "+":
res = num1 + num2
elseif op == ...
+ 1
Ok, it might take me a day to find it though.
+ 1
I was able to find it, the only problem is the part where it is supposed to print the answer out. Also, remember to treat it like it is a code from an IDE. Thanks!
https://sololearn.com/compiler-playground/cttA48djEUfg/?ref=app
+ 1
An example of what specifically?
+ 1
You won't get anything from ready-made code. Start by converting the inputs to float(). We'll go through it step by step.
0
Lisa, could you give me an example?
0
An example ofwhat the fixed code should look like.
0
Breathe bro...
don't worry, you got this...
This is what you should do...
ask yourself "what do I want to build??"
after having that clear idea of what you want ...in this case your calculator
ask yourself "how will I make this calculator"
make your research of everything you will need to build your calculator
this also includes the complexity...
"Am I making a standard calc with all 4 basic functions or a full scientific calculator????"
you need to satisfy these questions and many more to convince yourself of what you want
then you should ask yourself...
"what will I use to build itt"
"what are the challenges and risks involved in building calculator??" ETC
when you're done satisfying these curiosities , your calculator project is 75% done...Now what is left is to use all the answers you must have gathered to guide yourself to building your calculator.
This is the effective way to avoid this unexpected problem you're facing..
Good plan surely heads for success
0
Thanks Lionel!
0
I was able to make it, but the subtraction and addition don't work well.
https://sololearn.com/compiler-playground/chYZYqAX4pu5/?ref=app