Need help : Keyboard Input

I’m doing an assignment for my coding class, and my teacher referred us to this site. We ARE allowed copying codecs long as we cite our sources. But that’s not what I’m asking for. My assignment is to get input from a user how many iterations to do calculating pi. I would like to know how to get input from the user using a keyboard, and calculate the pi thing. It makes sense to me, if it makes sense to you great then it’d be awesome if you could help me further! But all I NEED to know is how to get input from a user using a keyboard, and do something with it. Thanks!

@JessicGriffin Here’s a simple example.


function setup()
    showKeyboard()
    print("Enter value")
end

function draw()
    background(40, 40, 50)
end

function keyboard(k)
    if k==RETURN then
        a=keyboardBuffer()
        hideKeyboard()
        showKeyboard()
        print(a)
    end
end

@JessicGriffin With Codea, pi can only be 5 digits past the decimal point. Not very interesting. So here’s another version showing pi to 75 digits. It’s not calculating it, but like I said, Codea only goes to 5 digits calculated.


supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
    a=0
    str="3.141592653589793238462643383279502884197169399375105820974944592307816406286"     
    showKeyboard()
end

function draw()
    background(40, 40, 50)
    fill(255)
    text("Enter a number (1-75)",WIDTH/2,HEIGHT/2+200)
    text("then press return",WIDTH/2,HEIGHT/2+175)
    if a>0 and a<76 then
        str1=string.sub(str,1,a+2)
        text(str1,WIDTH/2,HEIGHT/2+125)
    end
end

function keyboard(k)
    if k==RETURN then
        a=tonumber(keyboardBuffer())
        hideKeyboard()
        showKeyboard()
    end
end

Thanks a lot! I had no idea codea was limited to that :open_mouth: . I’ll have to include that when asking for the iteration. :slight_smile: Very helpful, I will be using bits of these codes in my assignment, will give credit. Thank you :smiley:

@JessicGriffin Very neat that your class is using Codea! Do you mind if I ask what level class and what country?

I’m in a grade 10 course in Canada. It’s the first year this course has used codea. We started out using scratch to practice and learn about programming, and now we moved onto using Lua. Half of our class is using CoronaSDK , and the other half is using Codea. Our final project will be to create a fully functioning app that grade three students have planned out for us. They plan and design, we code! :slight_smile: it’s a great class.

I wish my school had that

@dave1707 string.format("%.18f", 22 / 7) :wink:

@SkyTheCoder Nice trick, but the result is just a string, no different than str=“3.141592653589793238462643383279502884197169399375105820974944592307816406286” that I show above. Try converting it to a number with “tonumber()”. The original poster said “calculating pi”, so they won’t get more than 6-7 digits with 32 bit math.