Coding

Practical Coding Task.

Behind every credit card or ATM transaction are algorithms that test whether the PIN number you enter is correct or not. The main method used is to test for divisibility – i.e., ‘is this number divisible by another given number or not’?

This code challenge is about re-configuring the jumbled up Python code below so it works as a divisibility tester.  

 

y = int(input("Enter a denominator: ",))

print (x, "is divisible by", y,)

else:

x = int(input("Enter a numerator: ",))

if x % y == 0:

print (x, "is not divisible by", y,)

Method

1. Create a flow chart of how you think the program should work

Figure 21. Flow Chart


2. Produce “pseudocode” – code that can be used to sketch-out working code

INPUT A VARIABLE x <- int(input("Enter a numerator: ",))

INPUT A VARIABLE y <- int(input("Enter a denominator: ",))

IF x % y == 0:

   OUTPUT (x, "is divisible by", y,)

ELSE:

   OUTPUT (x, "is not divisible by", y,)

3. General purpose coding language – Python

x = int(input("Enter a numerator: ",))

y = int(input("Enter a denominator: ",))

if x % y == 0:

   print (x, "is divisible by", y,)

else:

   print (x, "is not divisible by", y,)

[TEMPORARY URL - https://trinket.io/python/3151e06923]

Open and run the code in “Trinket” here:


When the code is run, it should look like this.



Figure 22. Python code running in Trinket


Complete and Continue