User Tools

Site Tools


submissions:worksheet:databases:topic8

DATA ANALYTICS REFERENCE DOCUMENT

Document Title:Applied Databases - Topic 8 Worksheet
Document No.:1553616376
Author(s):Gerhard van der Linde
Contributor(s):

REVISION HISTORY


Revision


Details of Modification(s)

Reason for modification

Date

By
0 Draft releaseApplied Databases - Topic 8 Worksheet 2019/03/26 16:06 Gerhard van der Linde

Applied Databases - Topic 8

Question 1

W8_Q1.py
'''
Applied Databases - Topic 8 Exercise Sheet 
 
1. Write a Python program that has 2 arrays in the main function: 
    • One containing several elements which are numbers. 
    • The other empty. 
 
Write another function which accepts a number as a parameter and returns the number doubled. 
 
The main function should call this function for each element of the 1st array and populate the 2nd array with the doubled values. 
When the 2nd array is full it should be printed out. 
 
'''
 
def doublenum(num):
    return num*2
 
def main():
    arr1=[1,3,4,6,8,9,14,2]
    arr2=[]
    for i in range(len(arr1)):
        arr2.append(doublenum(arr1[i]))
    print(arr2)
 
if __name__ == "__main__":
    main()

Question 2

Q2.py
 
# Main function
def main():
	# Initialise array
	array = []
 
	display_menu()
 
	while True:
		choice = input("Enter choice: ")
 
		if (choice == "1"):
			array = fill_array()
			display_menu()
		elif (choice == "2"):
			print(array)
			display_menu()
		elif (choice == "3"):
			find_gt_in_array(array)
			display_menu()
		elif (choice == "4"):
			break;
		else:
			display_menu()
 
 
def fill_array():
# Write the necessary code to fill the array.
# -1 should not be part of the array
 
    arr=[]
    while True:
        choice=input("Enter value to add, -1 to end: ")
        if choice == "-1":
            return arr
        else:
            if choice.isnumeric()==True:
                arr.append(int(choice))
            else:
                print("Not a number, try again!")
 
 
def find_gt_in_array(array):
# Write the necessary code to get a number from the user
# and print out all numbers in the array that are greater
# than this number
    biggerVals=[]
    while True:
        choice=input("Find numbers greater than?: ")
        if choice.isnumeric() == True:
            biggerThan=int(choice)
            break
        else:
            print("Not a number, try again")
    for i in range(len(array)):
        if array[i]>biggerThan:
            biggerVals.append(array[i])
    print(biggerVals)
 
 
def display_menu():
    print("")
    print("MENU")
    print("=" * 4)
    print("1 - Fill Array")
    print("2 - Print Array")
    print("3 - Find > in Array")
    print("4 - Exit")
 
if __name__ == "__main__":
	# execute only if run as a script 
	main()
submissions/worksheet/databases/topic8.txt · Last modified: 2020/06/20 14:39 by 127.0.0.1