Getting to Know the Raspberry Pi – Session 4 – July 15, 2020

Introduction to Programming with Python

In this session we continued our introduction to basic programming concepts, including:

conditional operators: <, >, ==, !=, <=, >=

branching (if)

conditional loops (while)

functions (def)

The final code from is listed below:



def CountUp(CountTo):
    StartPoint=1
    while(StartPoint <= CountTo):
        print(StartPoint)
        StartPoint=StartPoint+1
        
def CountDown(CountTo):
    EndPoint=1
    while(CountTo >= EndPoint):
        print(CountTo)
        CountTo=CountTo-1


def main():
    menu=input("1 = count up, 2 = count down, q=quit: ")
    if(menu=="1"):
        #Call Count Up function
        sNumberUp=input("I will count from 1 to what number?")
        iNumberUp=int(sNumberUp)
        CountUp(iNumberUp)    
    elif (menu=="2"):
        #Call Count down function
        sNumberDown=input("I will count down to 1 from what number?")
        iNumberDown=int(sNumberDown)
        CountDown(iNumberDown)
    elif (menu=="q"):
        quit()
        
    else:
        print("Invalid Option")

#Not Part of main!
while(True):
    main()