word value NEED HELP
#1
Posted 27 February 2013 - 03:56 PM
the value of a word is (a = 1, b = 2, c = 3, d = 4....z = 26) so db == 6 (4+2)
I am getting an error at line 15 which i dont know why
this is supposed to print the value of the word.
pastebin get sz3v5Fv0
I have asked multiple people and I can not figure this out. Thanks
#2
Posted 27 February 2013 - 04:34 PM
EDIT: Hmm, really tired. Didn't even interperet his question correctly, nor did I see the pastebin link... Well with that derp, imma opt out of this thread
#3
Posted 27 February 2013 - 04:41 PM
that should get it working. Some other random tips to possibly improve the program:
instead of looping through an array of all letters to find the index of the right one, turn it around, make the table use the letters themselves as keys and have the numbers as values, ex:
abc={
a=1, b=2, c=3, ...etc
}
That way, instead of looping through the table, you can look up the value directly, ex, "abc["c"]" would give the value stored for c, 3.Also, a forum tip, you said the error was on line 15, but in the code as you actually put it on pastebin, the error was on line 13. This was either a mistake on your part, or you removed 2 lines somewhere in the program. You'll get quicker and better responses in the future if you double-check that the line number you report matches the code as you actually shared it. The full error message, rather than just the line number, would also be helpful.
#4
Posted 27 February 2013 - 08:41 PM
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
}
Function find(letter)
For i,j in pairs(chars) do
If j == letter then
return i
End
End
End
Local Word = read()
TotalCount = 0
For i = 1, str.len(word) do
Letter = Str.sub(word,i,i)
TotalCount = TotalCount + find(letter)
End
Print("that word is worth : "..TotalCount)
#5
Posted 27 February 2013 - 08:43 PM
X = table.insert
Just table.insert.
#6
Posted 27 February 2013 - 09:08 PM
nobody1717, on 27 February 2013 - 08:41 PM, said:
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
}
Function find(letter)
For i,j in pairs(chars) do
If j == letter then
return i
End
End
End
Local Word = read()
TotalCount = 0
For i = 1, str.len(word) do
Letter = Str.sub(word,i,i)
TotalCount = TotalCount + find(letter)
End
Print("that word is worth : "..TotalCount)
that is, conservatively, 50x more broken than the code in the OP, which had only the one minor error you noted in your follow-up post 2 minutes later.
#7
Posted 28 February 2013 - 12:37 AM
Try this Code it should do what you want. Used the Method Gopher suggested already cause i think this is the easiest way.
local function Countit()
abc={A=1, B=2, C=3, D=4, E=5, F=6, G=7, H=8, I=9, J=10 ,K=11, L=12, M=13, N=14, O=15, P=16, Q=17, R=18, S=19, T=20, U=21, V=22, W=23, X=24, Y=25, Z=26,a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10, k=11, l=12, m=13, n=14, o=15, p=16, q=17, r=18, s=19, t=20, u=21, v=22, w=23, x=24, y=25, z=26
}
local TotalCount =0
print("test")
local word=read()
for i=1, #word do
local Letter = string.sub(word,i,i)
TotalCount = TotalCount + abc[Letter]
end
print(TotalCount)
end
Countit()
Greets Loki
#8
Posted 28 February 2013 - 01:35 AM
local chars = {
["a"] = 1,
["b"] = 2,
["c"] = 3,
["d"] = 4,
["e"] = 5,
["f"] = 6,
["g"] = 7,
["h"] = 8,
["i"] = 9,
["j"] = 10,
["k"] = 11,
}
local function count( str )
local c = 0
for i = 1, #str do
c = c + chars[ str:sub( i, i ):lower() ]
end
return c
end
local textToCount = "CAB"
local textCount = count( textToCount )
print(textCount)
#9
Posted 28 February 2013 - 01:55 AM
The code:
local text = "abcd" local function count(str) str = str:upper() local count = 0 for i = 1, #str do local char = str:sub(i,i) count = count + char:byte() - 64 -- Capital A is ASCII 65 end return count end print(text) print(count(text))
Alternatively if you wanted to use a table, like remiXs solution, you could avoid typing it out by using a loop like so:
for i = 65, 90 do tTable[ string.char( i ) ] = ( i - 64 ) end
#10
Posted 01 March 2013 - 05:31 PM
GopherAtl, on 27 February 2013 - 09:08 PM, said:
nobody1717, on 27 February 2013 - 08:41 PM, said:
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
}
Function find(letter)
For i,j in pairs(chars) do
If j == letter then
return i
End
End
End
Local Word = read()
TotalCount = 0
For i = 1, str.len(word) do
Letter = Str.sub(word,i,i)
TotalCount = TotalCount + find(letter)
End
Print("that word is worth : "..TotalCount)
that is, conservatively, 50x more broken than the code in the OP, which had only the one minor error you noted in your follow-up post 2 minutes later.
Besides the capital letters (im too lazy to edit them out)
What is wrong with it?
#11
Posted 01 March 2013 - 05:53 PM
nobody1717, on 01 March 2013 - 05:31 PM, said:
Looks like you wrote it on your phone
a problem besides the capital letters is the table, each letter needs to be encased with inverted commas
#12
Posted 01 March 2013 - 05:55 PM
nobody1717, on 01 March 2013 - 05:31 PM, said:
- Its not in code tags.
- its 'end' not 'End'
- its 'str' not 'Str'
- its 'print' not 'Print'
- its 'local' not 'Local'
- basically you are using different cases all over the place.
- the elements in a table need to be in " " or ' ' EDIT: damn ninja'd on this one
- there is no need to use pairs for a simple table, use a incremental for loop instead, using a generic for loop confuses people
#13
Posted 02 March 2013 - 02:07 AM
local letters = 'abcdefghijklmnopqrstuvwxyz'
while true do
local input = read()
local total = 0
for v in input:gmatch '.' do
total = total + (letters:find(v:lower()) or 0)
end
print(total)
end
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











