Jump to content




[CC]Code Cleanup


  • You cannot reply to this topic
170 replies to this topic

#81 mrdawgza

  • Members
  • 110 posts
  • LocationSouth Africa

Posted 02 December 2013 - 05:54 PM

I guess I'll post new bad code since the last one wouldn't work out.

My code:
while true do
local event, button, x, Y = os.pullEvent()
 if event == "mouse_click" then
  if x>1 and x<10 and Y==1 and button==1 then
   print("Hi") 
  elseif X>1 and x<10 and y=2 and button ==1 then
   print("Hello")
  else
  end
 end
end

The error in that piece of code is a simple mistake I often make within my OS.

#82 jay5476

  • Members
  • 289 posts

Posted 03 December 2013 - 05:06 AM

you assign the return value to a lower case 'x' and then later try to use an uppercase 'X'
if " 2+2" == "4" then
  print("4")
end
yes its real simple :/

#83 mrdawgza

  • Members
  • 110 posts
  • LocationSouth Africa

Posted 03 December 2013 - 05:53 AM

View Postjay5476, on 03 December 2013 - 05:06 AM, said:

you assign the return value to a lower case 'x' and then later try to use an uppercase 'X'
-snip-
yes its real simple :/
Yes to that - but- also note for the 2nd Y; there is only one = instead of two.

Lemme clean your code:
[color=#000088]if[/color][color=#000000] [/color][color=#008800]"2+2"[/color][color=#000000] [/color][color=#666600]==[/color][color=#000000] [/color][color=#008800]"4"[/color][color=#000000] [/color][color=#000088]then --space before 2+2[/color]
[color=#000000]  [/color][color=#000088]print[/color][color=#666600]([/color][color=#008800]"4"[/color][color=#666600])[/color]
[color=#000088]end[/color]

Am I right? :P

Edited by OptimusSnorr, 03 December 2013 - 05:53 AM.


#84 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 03 December 2013 - 05:56 AM

Lemme clean your code:

if "2+2" == "4" then --space before 2+2
print("4")
end

;)

#85 mrdawgza

  • Members
  • 110 posts
  • LocationSouth Africa

Posted 03 December 2013 - 06:01 AM

View Postjay5476, on 03 December 2013 - 05:06 AM, said:

you assign the return value to a lower case 'x' and then later try to use an uppercase 'X'
-snip-
yes its real simple :/
Yes but not yes; note the 2nd Y only has one = instead of two? (It is y=1, it should be y==1)

Lemme clean your code.
if "2+2" == "4" then --The space before the 2+2?
  print("4")
end

Am I right?

Here is my code:
while true do
print("Press any key to see how much wood a woodchuck could chuck if a woodchuck could chuck wood.")
os.pullEvent("key)
print("A woodchuck could chuck no amount of wood since a woodchuck couldn't chuck wood.")
end
end

Nice joke there!! I guess..

-Edit- Cleaning code blocks.

Edited by OptimusSnorr, 03 December 2013 - 06:03 AM.


#86 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 03 December 2013 - 03:22 PM

Your code isn't intended but,
You forgot a ' " ' on the os.pullEvent() line.

You can't do maths on strings i think.

My code:
function obj()
  return {local num = 4}
end
Obj = obj()
print(Obj.num)


#87 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 03 December 2013 - 03:37 PM

View PostFreack100, on 03 December 2013 - 03:22 PM, said:

You can't do maths on strings i think.
Im prettysure you cant, its valid code but the statement always returns false.

#88 Alice

  • Members
  • 429 posts
  • LocationBehind you.

Posted 04 December 2013 - 11:59 AM

function obj()
  return (local num = 4)
end
Obj = obj()
print(Obj.num)

helloWorld == function ()
local text==hello
print(text)
end ()


#89 CastleMan2000

  • Members
  • 195 posts
  • LocationThe trashcan where all Undertale trash is

Posted 14 December 2013 - 03:14 PM

View PostDeath, on 04 December 2013 - 11:59 AM, said:

function obj()
  return (local num = 4)
end
Obj = obj()
print(Obj.num)

helloWorld == function ()
local text==hello
print(text)
end ()

function obj()
  local num = 4
  return num
end
Obj = obj()
aNumber = Obj()
print(aNumber)

helloWorld == function ()
local text=="hello"
print(text)
end
Not sure about the helloWorld == function () and end parts, as well as the Obj() = obj().

#90 Alice

  • Members
  • 429 posts
  • LocationBehind you.

Posted 14 December 2013 - 03:38 PM

helloWorld = function ()
local text = "hello"
print(text)
end
Yeah. since functions are variables, you can declare a variable's value as a function like I just did above.
Also, text == "hello" works only if it's in an if statement iirc

#91 bentallea

  • Members
  • 19 posts

Posted 18 December 2013 - 10:01 PM

data = "asdjgh askdfgjhskadfjg hsdfjgh aksrghj sdhgsdf gks"
if data:find(" ") == 4 then
  print('true')
end


#92 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 December 2013 - 10:12 PM

View PostDeath, on 14 December 2013 - 03:38 PM, said:

Yeah. since functions are variables, you can declare a variable's value as a function like I just did above.
and sometimes you need to define them that way.

however it should be noted that this
local one = function()
  --# statements
end
is not the same as this
local function one()
  --# statements
end

Edited by theoriginalbit, 18 December 2013 - 10:12 PM.


#93 Alice

  • Members
  • 429 posts
  • LocationBehind you.

Posted 18 December 2013 - 11:36 PM

Nice code to clean Bit.
And what is the difference?
for i=1; 10 do
print(i)
end


#94 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 December 2013 - 11:44 PM

View PostDeath, on 18 December 2013 - 11:36 PM, said:

what is the difference?
Lets see if I can find my massive writeup I've done in AaP.

#95 Alice

  • Members
  • 429 posts
  • LocationBehind you.

Posted 18 December 2013 - 11:46 PM

I don't check there often :P It was a quick question, I'll look myself. Is it under the Renewal Project?

#96 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 December 2013 - 11:50 PM

View PostDeath, on 18 December 2013 - 11:46 PM, said:

I don't check there often :P It was a quick question, I'll look myself. Is it under the Renewal Project?
Here it is take a look at the spoiler in the spoiler.

#97 Alice

  • Members
  • 429 posts
  • LocationBehind you.

Posted 18 December 2013 - 11:56 PM

Thanks
Also, reposting code

View PostDeath, on 18 December 2013 - 11:36 PM, said:

for i=1; 10 do
print(i)
end


#98 biggest yikes

  • Members
  • 573 posts

Posted 28 January 2014 - 05:13 PM

View PostMads, on 02 March 2013 - 03:09 AM, said:

while true do
	for i = 1, 1000 do
[...]
I know I'm late..But you forgot to correct the less than to greater than.
Lemme take my part now...


Fix:
for i = 1, 10 do
print(i)
end
Just a semi-colon.

Here's mine;

for i = 5, 999
print(Numb is; ".i.")
end

Edited by minimite, 28 January 2014 - 05:15 PM.


#99 Alice

  • Members
  • 429 posts
  • LocationBehind you.

Posted 30 January 2014 - 09:55 AM

Fixed:
for i=5, 999 do
 print('Number is: ' .. i)
end
New code:
for i=1, 100 do
if 'i' then print(I)
end


#100 CometWolf

  • Members
  • 1,283 posts

Posted 30 January 2014 - 11:34 AM

for i=1, 100 do
  print(i)
end

Come on guys, you're being given 15 lines to work with here, stop all doing the same thing...
local t1 = {}
local t2 = {
  derp = "test"
  1 = 2
}
setMetatable(t1,
  {__lt = function(table,val)
	return false
  end}
)
if t1 < t2 then
  print"You win"
else
  error"You fail, metatables ftw!" --Don't remove this line lol
end

Edited by CometWolf, 30 January 2014 - 05:32 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users