Week 3 - Activities


This week I worked on using variables that's value had been determined by user input fields.

1. Making decisions!

This code:

- Declares an integer variable and sets its initial value to 0;

- Asks the user to enter a number, then stores the entered number in your variable; and finally

- Tells the user what their number was.


This code then checks whether the users input is less than 12 and tells them the answer:


2. More decisions!

This code does the following:

a) If the number entered by the user is lower than 7, the message “lower than 7” is printed to the console;

b) Otherwise, if the number is 7, “It is 7” is printed; and

c) Otherwise, the message “more than 7” is printed.


This code:

Takes two integers given by the user and compares them, displaying whether one is larger, smaller or they are both equal.


This code is the same as the previous, however, it has been expanded to 3 inputted integers. The more inputs given the more complicated it becomes as in order to give the correct response after checking the smallest number, the programmer must try and account for all possible outcomes.



3. More Python Conditionals!

This code is using conditional If statements to describe the value of a variable. 


When using conditionals in python you must not forget to use semi colons and indents to help the program to understand which lines of code are belong to which outcome.


4 and 5. Short Circuit and Guard evaluation!

Short circuiting can be used to help guard against errors and the overuse of data and memory as by putting a system in place to stop a process early it will no longer spend unnecessary time and memory on a process that we know could result in an error. For example:

Test1

x = 6 

y = 0

print(x >= 2 and y != 0 and (x/y) > 2)

# This would give False

 Test2

x = 6

y = 0

print(x >= 2 and (x/y) > 2 and y != 0)

# This would give an Error!

In Test 1  y != 0 is placed first in order to intercept, as we know that if y = 0, allowing it to test (x/y) > 2 would result in an error as 6 cannot be divided by 0.

Whereas in Test 2 y != 0 is placed after (x/y) > 2 meaning there is nothing stopping it from erroring out.


8. Loops!

Here this For loop outputs the print function 6 times:


9. More loops!

This was fairly simple as all I needed to do was find a way to have the for loop count through each item in the list, which I was able to do by replacing the number in the brackets of DaysOfTheWeek[] with i that is already counting upwards.

10. Work out this!

On this code I had to change a lot of the code after speaking to another classmate. This task actually caused me a lot of confusion but after a lot of struggle I managed to add an if condition that checks which day in the list is currently being selected and attributes the correct suffix to them when finally printed out at the end. 

 

Leave a comment

Log in with itch.io to leave a comment.