A First World Problem

Posted by Neville on April 15, 2016 under How To, Python, Resources, Training | Comments are off for this article

Going With Python

After a break of more than 2 years, this site is back in action. The course mentioned in the last post was completed, but unfortunately other things got in the way and nothing much was done to build on the learning that came from participation.

Python CodingUntil about a month ago, when the second part of the MIT MOOC began. That course relies on having a reasonable knowledge and ability to program with Python. Consequently I have not been able to keep up with the Problem Sets and quizzes for the current course.

Being enrolled in the second part has encouraged me to go back to the original and brush up on the content, while attempting to code some simple (or what I thought would be simple) little tasks. There is SO MUCH to come to grips with, but the challenge is worth the effort.

Hello World: A First World Problem

As is usual for most early lessons in courses that teach computer programming, the first instructions get the student to print the phrase “Hello World”.

For those who have become a little jaded by having to do that for programming and other coding tasks this first step will give the student the option to bypass that trivial task.

The code below, written using Python, that can be run in the Python Interactive DeveLopment Environment (IDLE), will be used an example from which many different aspects of the Python language, and programming with Python (in the early stages).

 

The following links will take you to pages where the snippets of code will be explained and further examples and practice task will be presented.

First we will look at the last 2 lines of code. These are user-defined commands that tell the computer to go to to those 2 functions, that were defined earlier in the code above.

introDisplay()
userInput()

 

”’ Step01:

This file will run on Python version 3.5 and would need to be
edited for running on earlier versions.
”’

import random
#imports the random methods to use later.
def helloWorld():

    #helloWorld is a simple function that prints the string ‘Hello World!’ to the screen.

    print(‘Hello world!’)
    print(‘What is your name?’)
    myName = input()
    myName = myName.title()
    print(‘It is good to meet you, ‘ + myName)
    return

def fibNums(x):

     if x == 0 or x == 1:
         return 1
     else:
         return fibNums(x-1) + fibNums(x-2)

def getFib(n):

    for i in range(n+1):

        print(‘fib of ‘ + str(i) + ‘ = ‘ + str(fibNums(i)))

def introDisplay():
     #prints the introduction to screen
     print (‘Learn To Program courses usually start with ‘)
     print (‘teaching how to print “Hello World” on the screen.’)
     print (”)
     print (‘In this course you can skip that step….’)
     print (‘Or you can take to usual path.’)
     print (”)
    return

def userInput():
     # This function asks if the user wants to print “Hello World”,
     #   If so it does that, otherwise simply exits the function.
     print(‘Do you want to see Hello World printed on the screen? (yes or no)’)
     if input().lower().startswith(‘y’):

        helloWorld()
     else:
         print (‘You skipped the Hello World step…..’)
         print (‘Or did you?’)
        

          if random.random()> 0.4:
            #get some fibonacci numbers
             print (“Here’s some Fibonacci numbers to look at….”)
             getFib(10)

        else:
            print (‘Computer says “No”‘)
            helloWorld()

    print (“That’s it! I’m Done.”)

    return

introDisplay()
userInput()

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Comments are closed.