0
Make a calculator on python
Itâs a design of calculator including (+,-,*,/) and input an operator with spaces before and after the operator. The code was written by others. Could someone who is sharper explain and teach me the logic of following code in detail? I canât figure it out by myself:( Iâm really grateful for any help!! https://code.sololearn.com/cdnKimeBMTUc/?ref=app
14 Respostas
0
When you assign i:
for i in range(len(elements)):
it takes a sequence of values from 0 to the lenght of elements list - 1, so it will change from 0 to 2 with each iteration.
With each iteration your code tries to find '*' in the array elements. With first iterations it doesn't find it, so i just increases from 0 to 1 and your code skips calculation routine.
With the second iteration it finds '*', performs calculations and _pops two list elements off_ (calls pop_twice function). Now your array has lenght 1, not 3 as in the beginning. But your i still increases with next iteration and becomes 2.
Obviously, python can not find in an array with length = 1 an element with index=2, and your script fails with out-of-range-error.
That is why you had 'break' statements in your original code.
+ 1
Thanks a lot! I understand! :)
+ 1
I just programmed one but canât get it to work. Any tips?
https://code.sololearn.com/cj643110e0q9/?ref=app
+ 1
Finished it
0
This code has an error (a typo) in line 15 (it must be:
elements[i-1]=float(elements[i-1]) / float(elements[i+1])
not:
elements[i-1]=float(elements[i-1])+float(elements[i+1])
or the function will return an erroneous result.
0
@strawdog could you teach me why it can use âpopâ two times in a row? I thought whenever use it to pop one object, it cannot to be used again to pop something else in case itâs out of range?
0
Also, why can we use pop to process this exercise?
0
Vicky Kuo
Pop just pops an element from a list returning it as a result. If you use it without parameter, it pops and returns the last element of a list. If you add parameter, it will pop the element which index equals to that parameter.
>>> a = [1,2,3,4,5]
>>> a.pop()
5
>>> print(a)
[1, 2, 3, 4]
>>> a.pop(1)
2
>>> print(a)
[1, 3, 4]
>>>
So you can definitely use pop as many times as your list allows it.
0
strawdogđđ do you know why the following code is out of range?
def calculator(s):
def pop_twice(lst,index):
lst.pop(index+1)
lst.pop(index)
elements=s.split(" ")
while len(elements)>2:
if "*" in elements or "/" in elements:
for i in range(len(elements)):
if elements[i]=="*":
elements[i-1]=float(elements[i-1]) * float(elements[i+1])
pop_twice(elements,i)
return float(elements[0])
calculator("101 * 50")
0
Tiberio
1. You have an error in identation in line 11. remove all spacese before 'print' and add them again manually to make a proper ident.
2. You missed '+' in line 15 before 'result'
3. You cannnot use 'break' if you are not in a loop. To exit program try to use 'pass' instead of break. Or wrap the code into a function and use 'return'. Or raise SystemExit exception
Note, your program will produce a non-fatal error if user inputs 'quit'.
Your imput block is a little bit clumsy, I believe you ought to rewrite it.
0
strawdog you are awesome!!đđ
0
Thank you strawdog, I started learning python so I am still a complete noob but Iâll try to rewrite it
0
https://code.sololearn.com/ctPWyg4Mb5RP/?ref=app
I put a comment to thank you, strawdog
0
Vicky Kuo
1. Using space between an unary operator and its operand is highly depricated in programming community. If the user is quite accurate to put minus sign without a space before a number, your script will work just fine.
2. If you do not rely on user decency (wich is, unfortunately, a right approach), you need to use input check, i.e. with the help of the regular expressions, like:
>>> import re
>>> a = "10 - 11 + 4 / - 3"
>>> b = re.sub(r'([^\s\d]\s)-\s(\d+)', r'\1-\2', a )
>>> print(b)
10 - 11 + 4 / -3