Magic 8 Ball w/ JSON and Python



  • OK, so I did a very simple "magic 8 ball" app in Python. It is a springboard to using the OLED expansion board on my Intel Edison (which has buttons I want to utilize to make it a standalone app that will refresh (or just run again) when a button is pressed on the expansion board. I have a lot to do to get the Edison up and working, so I am doing my dev on the Omega2+ and so far, from the command line, it is working great. Again, nothing too outstanding, but another stepping stone.

    Here is the Python code. I happen to have Python3 installed.

    #!/usr/bin/env python
    
    from OmegaExpansion import oledExp
    import random
    import json
    
    getnum = "answer"+str(random.randint(1,20))
    with file('/smb/scripts/magic8ball.json', 'r') as answerfile:
    	answer = json.load(answerfile)
    
    oledExp.driverInit()
    oledExp.setCursor(0, 0)
    oledExp.write("+-------------------+")
    oledExp.setCursor(1, 0)
    oledExp.write("|Magic 8 Ball Speaks|")
    oledExp.setCursor(2, 0)
    oledExp.write("+-------------------+")
    oledExp.setCursor(4, 0)
    oledExp.write(answer[getnum])
    oledExp.setCursor(7, 0)
    oledExp.write("+-------------------+")
    
    exit()
    
    

    Here is the .json with the answers.

    {
    	"answer1": "It is certain.",
    	"answer2": "It is decidedly so.",
    	"answer3": "Without a doubt.",
    	"answer4": "Definitely yes.",
    	"answer5": "You may rely on it.",
    	"answer6": "As I see it, yes.",
    	"answer7": "Most likely.",
    	"answer8": "Outlook good.",
    	"answer9": "Yes.",
    	"answer10": "Signs point to yes.",
    	"answer11": "Reply hazy try again.",
    	"answer12": "Ask again later.",
    	"answer13": "Better not tell you now.",
    	"answer14": "Cannot predict now.",
    	"answer15": "Concentrate and ask again.",
    	"answer16": "Don't count on it.",
    	"answer17": "My reply is no.",
    	"answer18": "My sources say no.",
    	"answer19": "Outlook not so good.",
    	"answer20": "Very doubtful."
    }
    


Looks like your connection to Community was lost, please wait while we try to reconnect.