+ 4

So, I thought you could use variables in python but................. o.o

Hi again fellows :) So I have this code i've made up, wich is step one to a home-made little calculator. It is supposed to work, but my while loop seems to consider that my variables are not declared, and I really don't get why o.o so my code is here : https://code.sololearn.com/cFS7YVKQ4fcP and I enter 15+25-30 for the testing (wich is supposed to print 10, logically. But instead i get this message : >>>>>>>> Traceback (most recent call last): File "..\Playground\", line 69, in <module> Add_Sub() File "..\Playground\", line 12, in Add_Sub while counter<=len(base_calc): UnboundLocalError: local variable 'counter' referenced before assignment <<<<<<<< But why in Pete's name do have I this error showing up ? I really don't get it :(

2nd Mar 2018, 1:50 AM
Clement combes
Clement combes - avatar
6 odpowiedzi
+ 2
You complicated yourself with poor structured code. 1st error : You misused attribute 'base_calc'. You get input from user (which is calculation string) and assign it to **global** base_calc. However, you use **local** base_calc in function Add_Sub(). You should'nt define global variable if you want to use local variable and vice visa. 2nd error : Just i said above, your code is complicated with unnecessary code lines. You become don't know what you are doing. A little piece of advice: Avoid using global variable as much as you can in your code. (just know global persist until program end, while local end with local(i.e in your case Add_Sub function) ) Here's your code (Cleaner version) https://code.sololearn.com/cG52Fj22r3pd/?ref=app
2nd Mar 2018, 7:18 AM
Sylar
+ 4
ok.... 🤔 thank you all for your time, there's a lot of thinking to do ! I've worked a lot on C lately and clearly the philosophy is really different ^^' edit: no it's not, I Just mentaly placed my 'main' in python above everything when in fact it is Just like in C, so Just above the first who's not à function Def o.o thanks again, now I totally get it ❤
2nd Mar 2018, 12:11 PM
Clement combes
Clement combes - avatar
+ 3
python has a default function for what u r trying to do.check my code sorry but ur code has too many mistakes https://code.sololearn.com/cYiUK7n4QGVL/?ref=app
2nd Mar 2018, 4:03 AM
Siva Shankar
Siva Shankar - avatar
+ 2
well, I'm trying to make an autoderminate calculator, where you just type whatever calculation you want and get a response. kind of like in Excel... but even tho I'm making modifications I keep discovering New errors. now it's the index of counter Who doesn't fit, I don't get it o.o
2nd Mar 2018, 2:33 AM
Clement combes
Clement combes - avatar
+ 1
you might try and declare the variables in the function and make the function take in the necessary calculation as an argument p.s. your code is weird af
2nd Mar 2018, 2:05 AM
Markus Kaleton
Markus Kaleton - avatar
0
Currently, you are setting base_calc equal to an empty string then trying to access individual offsets based on counter.
2nd Mar 2018, 3:41 AM
Jesse Bayliss
Jesse Bayliss - avatar