dj0965
dj0965

Reputation: 135

repeat until loop in lua

What Am I doing wrong with my lua code?

local which

print("Type f to convert fahrenheit to celsius and c to convert celsius to fahrenheit")
which = io.read()
repeat
    if which=="f" then
        local c
        local f
        print("input your fahrenheit temperature")
        f = tonumber(io.read())
        c = (f-32)/1.8
        print(c)
    end

    elseif which=="c" then
        local ce
        local fa
        print("input your celsius temperature")
        c = tonumber(io.read())
        f = (c*1.8)+32
    end

    else do
    print("Type f to convert fahrenhiet to celsius and c to convert celsius to fahrenheit")
until which=="f" or which=="c"

Upvotes: 0

Views: 6975

Answers (3)

Jim
Jim

Reputation: 1

local which
repeat
    print("Type f to convert fahrenheit to celsius and c to convert celsius to fahrenheit")
    which = io.read()
    if which=="f" then
        local c
        local f
        print("input your fahrenheit temperature")
        f = tonumber(io.read())
        c = (f-32)/1.8
        print(c)

    elseif which=="c" then
        local c
        local f
        print("input your celsius temperature")
        c = tonumber(io.read())
        f = (c*1.8)+32
        print(f)
    end
    print("do you want to play again? y/n?")
    antwort = io.read()


until antwort ~= "y"

Upvotes: 0

hjpotter92
hjpotter92

Reputation: 80657

You are closing your if block first. Remove the end statements which you have used to close if and elseif and put it just to close after else.

local which

print("Type f to convert fahrenheit to celsius and c to convert celsius to fahrenheit")
which = io.read()
repeat
    if which=="f" then
        local c
        local f
        print("input your fahrenheit temperature")
        f = tonumber(io.read())
        c = (f-32)/1.8
        print(c)

    elseif which=="c" then
        local ce
        local fa
        print("input your celsius temperature")
        c = tonumber(io.read())
        f = (c*1.8)+32

    else
        print("Type f to convert fahrenhiet to celsius and c to convert celsius to fahrenheit")
    end
until which=="f" or which=="c"

P.S.: This might lead you to infinite loop. You need to update which after every iteration inside the repeat until.

Upvotes: 3

Martin Ender
Martin Ender

Reputation: 44289

There should be no end before elseif. There should also be no end before and no do after else. And there should be an end after the else part and before until:

repeat
  if ... then
    ...
  elseif ... then
    ...
  else
    ...
  end
until ...

Next time it would be helpful if you posted at least what your problem is (error message, unexpected output, etc.).

Upvotes: 1

Related Questions