Tag: Quit

National 5 – Boolean

A boolean variable is used to store True or False values. This can be very useful in programs, as you can use it in the program logic.

In the example below the user has to choose to exit the program, this sets the quitProg boolean variable to True which makes the result of the condition in line 3 False. Notice that True/False have capitals at the start of the value.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
quitProg=False

while not quitProg:

    #Program does stuff here
    
    choice=input('Do you want to quit (Y/N):')
    if choice in 'Yy':
        quitProg=True

print('Exiting program')    

Using a message box to make a choice

It is possible to use a MsgBox to make a choice in your program. You first of all assign a integer variable to the MsgBox then you change the buttons attributes to vbYesNo. vbYesNo is a VB constant that represents a value, the use of the constant means that you do not need to memories the number, vbYes and vbNo are other examples of these VB constants. An IF statement is then used to make the selection.

N5