Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Getting errors when incrementing a variable
Started by filippo66627 Jan 20 2020 10:26 AM

13 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

filippo66627

[GLOBAL: userInfoPane.html]
filippo66627
  • Enthusiast

  • 66 posts
  • Corona SDK

Hi guys and ty for constantly hepling me, im getting the error i attacched when i try to do this : 

local risultato2 = risultato 2 + risultato
 

 

Attached Files



[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,959 posts
  • Corona SDK

The error is exactly what it is telling you - risultato2 is nil because it hasn't been defined yet (I assume the gap in your example is a typo)

 

 
local risultato2 = 0
 
risultato2 = risultato2 + risultato
 


[TOPIC: post.html]
#3

jdsmedeirosbr

[GLOBAL: userInfoPane.html]
jdsmedeirosbr
  • Enthusiast

  • 94 posts
  • Corona SDK

To remove the space in risultato 2

[TOPIC: post.html]
#4

filippo66627

[GLOBAL: userInfoPane.html]
filippo66627
  • Enthusiast

  • 66 posts
  • Corona SDK

ty guys anyway i figured out how to solve it. 

Now theres another problem tho: 

--funzione per il numero 0 
local function handleButtonEvent0 ( event )
  local phase = event.phase 
  if "ended" == event.phase then
    numericField.text = numericField.text.."0"
       local risultato = tonumber(numericField.text) 
    
  end 
end
i have these handlebuttonevents for buttons between 0 and 9, whenever i press "-" on the calculator tho 
--funzione per la sottrazzione 
local function handleButtonEventSottr ( event )
  local phase = event.phase 
  if "ended" == event.phase then 
  
    numericField.text = numericField.text.."-"
      local risultato2 = risultato - risultato2
      
     
 
  end 
end
it gives me 0 so i tried
print (risultato)
to see if that was the problem and infact risultato = 0, can anyone help me out ? tysm 


[TOPIC: post.html]
#5

filippo66627

[GLOBAL: userInfoPane.html]
filippo66627
  • Enthusiast

  • 66 posts
  • Corona SDK

 

The error is exactly what it is telling you - risultato2 is nil because it hasn't been defined yet (I assume the gap in your example is a typo)

 
local risultato2 = 0
 
risultato2 = risultato2 + risultato
 

yea i did this but risultato is equal to 0 and idk why 



[TOPIC: post.html]
#6

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,959 posts
  • Corona SDK

You need to look into scope. Again, this is because you're trying to do things before you have learnt the basics of lua, like trying to write a book in French before you know, well, French.



[TOPIC: post.html]
#7

filippo66627

[GLOBAL: userInfoPane.html]
filippo66627
  • Enthusiast

  • 66 posts
  • Corona SDK

You need to look into scope. Again, this is because you're trying to do things before you have learnt the basics of lua, like trying to write a book in French before you know, well, French.

ty anyway 



[TOPIC: post.html]
#8

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,959 posts
  • Corona SDK

The very basic premise is that a local variable is only visible to the 'scope' it is defined in.

 

 
local variable_ONE = 4 -- I am defined at the file-level, I am visible to everything below me in this lua file.
 
 
local myFunction = function ()
 
  local variable_TWO = 3 -- I am defined at function-level, I am visible to everything below me inside this function. My value is forgotten as soon as the function ends.
 
  variable_ONE = 5 -- this is how to change variable_ONE so it is visible outside this function.
 
 local variable_ONE = 2
-- I am a completely different variable to the one above, albeit with the same name. You can no longer access the one above in this function, and changing me has no effect on the one above. 

end
 
 



[TOPIC: post.html]
#9

filippo66627

[GLOBAL: userInfoPane.html]
filippo66627
  • Enthusiast

  • 66 posts
  • Corona SDK

The very basic premise is that a local variable is only visible to the 'scope' it is defined in.

 

 
local variable_ONE = 4 -- I am defined at the file-level, I am visible to everything below me in this lua file.
 
 
local myFunction = function ()
 
  local variable_TWO = 3 -- I am defined at function-level, I am visible to everything below me inside this function. My value is forgotten as soon as the function ends.
 
  variable_ONE = 5 -- this is how to change variable_ONE so it is visible outside this function.
 
 local variable_ONE = 2
-- I am a completely different variable to the one above, albeit with the same name. You can no longer access the one above in this function, and changing me has no effect on the one above. 
end
 
 

first of all ty. Im still having trouble with this calculator.. i cant find a way to actually do subtractions when the minus button is pressed, sums when the + is pressed etc.. this is my code could u help me out 

 

  local composer = require( "composer" )
 
local scene = composer.newScene()
 
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
 
local risultato = 0 
local risultato2 = 0 
 
 local numericField = native.newTextField ( 162 , 90 ,315 , 50 ) 
numericField.text = ""
 
 
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
 
-- create()
function scene:create( event )
 
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
device_width = display.contentWidth --larghezza in pixel dello schermo del tel
device_height = display.contentHeight  --altezza in pixel dello schermo del tel
local coomposer = require ("composer")
--impostazione sfondo 
local sfondo = display.newImageRect( "sfondonero.png",device_width,device_height)
 
sfondo.x= device_width/2
sfondo.y= device_height/2
 
--richiamo libreria per i widgets 
local widget = require("widget")
 
 
 
 
 
 
 
--funzione che viene richiamata quando il bottone viene premuto quindi quando è nello stato "ended" 
 
 --funzione per il numero 1 
local function handleButtonEvent1 ( event )
  local phase = event.phase 
  if "ended" == event.phase then 
    if risultato2 ~= 0 then 
      risultato = 0
    numericField.text = numericField.text.."1"
  risultato = tonumber(numericField.text) 
else 
      numericField.text = numericField.text.."1"
  
  risultato = tonumber(numericField.text)
  end 
end 
end
 
--funzione per il numero 2 
local function handleButtonEvent2 ( event )
  local phase = event.phase 
  if "ended" == event.phase then 
    if risultato2 ~= 0 then 
      risultato = 0
    numericField.text = numericField.text.."2"
  risultato = tonumber(numericField.text) 
else 
      numericField.text = numericField.text.."2"
  
  risultato = tonumber(numericField.text)
  end 
end 
end
 
--funzione per il numero 3 
local function handleButtonEvent3 ( event )
  local phase = event.phase 
  if "ended" == event.phase then
    if risultato2 ~= 0 then 
      risultato = 0
    numericField.text = numericField.text.."3"
risultato = tonumber(numericField.text) 
else 
      numericField.text = numericField.text.."3"
  
risultato = tonumber(numericField.text)
  end 
end 
end
 
 --funzione per il numero 4 
 
local function handleButtonEvent4 ( event )
  local phase = event.phase 
  if "ended" == event.phase then 
    if risultato2 ~= 0 then 
      risultato = 0
    numericField.text = numericField.text.."4"
    risultato = tonumber(numericField.text) 
else 
      numericField.text = numericField.text.."4"
  
risultato = tonumber(numericField.text)
  end 
end 
end
 
--funzione per il numero 5 
local function handleButtonEvent5 ( event )
  local phase = event.phase 
  if "ended" == event.phase then 
    if risultato2 ~= 0 then 
      risultato = 0
    numericField.text = numericField.text.."5"
  risultato = tonumber(numericField.text) 
else 
      numericField.text = numericField.text.."5"
  
  risultato = tonumber(numericField.text)
  end 
end 
end
 
--funzione per il numero 6 
local function handleButtonEvent6 ( event )
  local phase = event.phase 
  if "ended" == event.phase then 
 
    if risultato2 ~= 0 then 
      risultato = 0
    numericField.text = numericField.text.."6"
  risultato = tonumber(numericField.text) 
else 
      numericField.text = numericField.text.."6"
  
  risultato = tonumber(numericField.text)
  end 
end 
end
 
--funzione per il numero 7
local function handleButtonEvent7 ( event )
  local phase = event.phase 
  if "ended" == event.phase then 
    if risultato2 ~= 0 then 
      risultato = 0
    numericField.text = numericField.text.."7"
  risultato = tonumber(numericField.text) 
else 
      numericField.text = numericField.text.."7"
  
  risultato = tonumber(numericField.text)
 
  end 
end 
end 
 
--funzione per il numero 8 
local function handleButtonEvent8 ( event )
  local phase = event.phase 
  if "ended" == event.phase then
    if risultato2 ~= 0 then 
      risultato = 0
    numericField.text = numericField.text.."8"
  risultato = tonumber(numericField.text) 
else 
      numericField.text = numericField.text.."8"
  
  risultato = tonumber(numericField.text)
  end 
end 
end
 
--funzione per il numero 9
local function handleButtonEvent9 ( event )
  local phase = event.phase 
  if "ended" == event.phase then
    if risultato2 ~= 0 then 
      risultato = 0
    numericField.text = numericField.text.."9"
  risultato = tonumber(numericField.text) 
else 
      numericField.text = numericField.text.."9"
  
  risultato = tonumber(numericField.text)
 
end 
  end 
end 
 
--funzione per il numero 0 
local function handleButtonEvent0 ( event )
  local phase = event.phase 
  if "ended" == event.phase then
    if risultato2 ~= 0 then 
      risultato = 0
    numericField.text = numericField.text.."0"
  risultato = tonumber(numericField.text) 
else 
      numericField.text = numericField.text.."0"
  
  risultato = tonumber(numericField.text)
  end 
end 
end
 
 
--funzione ritorno home 
local function handleButtonEventHome (event)
  local phase = event.phase 
  if "ended" == event.phase then 
    composer.gotoScene("Menu2")
  end 
end 
 
--funzione punto decimale 
local function handleButtonEventDec ( event )
  local phase = event.phase 
  if "ended" == event.phase then 
    numericField.text =numericField.text.. "."
  end 
end 
 
--funzione per la sottrazzione 
local function handleButtonEventSottr ( event )
  local phase = event.phase 
  if "ended" == event.phase then 
  
    numericField.text = numericField.text.."-"
      risultato2 = risultato - risultato2 
      
       print(risultato2)
 
  end 
end 
 
--funzione per la divisione 
local function handleButtonEventDiv ( event )
  local phase = event.phase 
  if "ended" == event.phase then 
 
    numericField.text = numericField.text.."/"
      risultato2 = risultato / risultato2 
      
       print(risultato2) 
  end 
end 
 
--funzione per l'addizzione 
local function handleButtonEventAdd ( event )
  local phase = event.phase 
  if "ended" == event.phase then
 
    numericField.text = numericField.text.."+"
      risultato2 = risultato + risultato2 
      
       print(risultato2)
    
  end 
end 
 
--funzione per la moltiplicazione 
local function handleButtonEventMolt ( event )
  local phase = event.phase 
  if "ended" == event.phase then 
 
    numericField.text = numericField.text.."x"
      risultato2 = risultato * risultato2 
      
       print(risultato2)
  end 
end 
 
--funzione per l'uguale 
local function handleButtonEventUguale ( event )
  local phase = event.phase
  if "ended" == event.phase then 
    numericField.text = risultato2..""
   
  end 
end 
 
--funzione per il cambio di scena 
local function handleButtonEventMenu3 ( event )
  local phase = event.phase 
  if "ended" == event.phase then 
    composer.gotoScene ("Menu3")
  end 
end   


[TOPIC: post.html]
#10

pixec

[GLOBAL: userInfoPane.html]
pixec
  • Contributor

  • 154 posts
  • Corona SDK

Here is my code. Fix it for me. Thanks. That is a bit too lazy!! :D

[TOPIC: post.html]
#11

filippo66627

[GLOBAL: userInfoPane.html]
filippo66627
  • Enthusiast

  • 66 posts
  • Corona SDK

Here is my code. Fix it for me. Thanks. That is a bit too lazy!! :D

its just that i can't figure out a way to make this working out 



[TOPIC: post.html]
#12

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,169 posts
  • Corona SDK

Well, first of all, you want to eliminate any unnecessary code.

 

For instance, you seem to do this in your every button listener:

local function handleButtonEventMenu3 ( event )
  -- You create a local variable, phase, but you never use it. Get rid of it or start using it.
  local phase = event.phase
  if "ended" == event.phase then 
    composer.gotoScene ("Menu3")
  end 
end  

Then, you should consider combining as many similar functions as possible under the same function. Instead of having a different function for adding, subtracting, multiplying and dividing, etc. just have a single function that checks what operation the user wishes to carry out and then carry them out in the same function.



[TOPIC: post.html]
#13

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,959 posts
  • Corona SDK

No prizes for guessing the next question....



[TOPIC: post.html]
#14

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,194 posts
  • Corona SDK

maybe how to implement standard deviation?




[topic_controls]
[/topic_controls]