TB1 Week 4
number = int(0)
number = input("Enter a number:")
print("Your number is:" + number)
number = int(number)
#converts the value input from a string to an integer
if number < 12:
print("Your number is less than 12")
Modified version
number = int(0)
number = input("Enter a number:")
print("Your number is:" + number)
number = int(number)
#converts the value input from a string to an integer
if number < 7:
print("Your number is less than 7")
elif number == 7:
print("The number is 7")
else:
print("Your number is more than 7")number = int(0)
number = input("Enter a number:")
print("Your number is:" + number)
number = int(number)
#converts the value input from a string to an integer
if number < 7:
print("Your number is less than 7")
elif number == 7:
print("The number is 7")
else:
print("Your number is more than 7")
new script that this time takes two integers as input from the user, and tells them which one has the smaller value.
number_1 = int(0)
number_2 = int(0)
number_1 = input("Enter a number:")
number_2 = input("Enter another number:")
if number_1 > number_2:
print(str(number_1) + " is greater than " + str(number_2))
#converts the number into a string so it can be written
elif number_2 > number_1:
print(str(number_2) + " is greater than " + str(number_1))
else:
print("These numbers are the same")
If I had to scale the code up for three or four inputs, I would have to define more variables and have more input statements.
a) declare a variable called y, which is initialised to a value between 1 and 100 (you can choose this value), then
b) write and test short scripts to do each of the checks below. Make sure that your scripts look professional, e.g. include full comments within them explaining their functionality (Note: full commenting of your scripts will be expected for all the artefacts you submit with your TOFA portfolio).
y = 15
if y == 1:
print("y is 1")
#checks if y is 1
elif y > 5:
print("y is high")
#checks if y is greater than 5
elif y < 5:
print("y is low")
#checcks if y is less than 5
elif y != 7:
print("y is unlucky")
#checks if y does not equal 7
elif y == 2 or 3:
print("y is 2 or 3")
#checks that y equals 2 or 3
elif y > 4 and y <= 7:
print("y is mid-range")
#checks if y is greater than 4 and less than or equal to 7
Python has a feature in which it reads expressions from left to right and will stop reading expressions if it detects there is nothing to be gained from it, as it has already come up with an answer, this is known as short-circuiting the evaluation. (more info https://www.geeksforgeeks.org/short-circuiting-techniques-python/)
x = 1
y = 0
print(x >= 2 and (x/y) > 2)
Running this code reveals a FALSE as the first expression is false.
x = 6
y = 0
print(x >= 2 and (x/y) > 2)
However running this code gives an error as you cannot divide by zero, and although the previous example had a division by zero, it was not ran as the first expression was read as false and the rest of the expression was never executed as part of the short circuit rule.
You can also set up expressions that will prevent errors from forming, these are known as ‘guard evaluations’. For example:
x = 6
y = 0
print(x >= 2 and y != 0 and (x/y) > 2)
# This would give False
x = 6
y = 0
print(x >= 2 and (x/y) > 2 and y != 0)
# This would give an Error!
These two pieces of code are very similar, but whilst the first gives a false, the second gives an error despite being nearly identical. This is because the y != 0 allows the code to check that y is a non-zero number before it tries to divide it, which would cause an error, this is a guard evaluation.
Lists are used to store multiple values inside of them, they start from 0 and then increment upwards (more info https://www.w3schools.com/python/python_lists.asp)
listOfLights = ["keyLight", "sideLight", "backLight", "fillLight"]
#lists start from 0 then go up
print(listOfLights[0])
#prints the 'first' item in the list
print(listOfLights[2])
#prints the second
print(listOfLights[3])
#prints the third
print(listOfLights[-1])
#prints the value that is just before the first, so loops back around
print(listOfLights[0:2])
#prints both the first and second in the list despite 2 being assigned to the third item in the list
print(len(listOfLights))
#prints the number of values in the list
You can also modify lists using built in functions known as methods, an example.
listOfLights = ["keyLight", "sideLight", "backLight", "fillLight"]
listOfLights.sort()
#sorts by alphabetical order
print(listOfLights)
listOfLights.reverse()
#reverses the order of the list
print(listOfLights)
listOfLights.append("newLight")
#adds an additional value to the list
print(listOfLights)
For loops are used to iterate over a sequence. (more info https://www.w3schools.com/python/python_for_loops.asp)
python = ["Python is awesome!", "Python is awesome!", "Python is awesome!", "Python is awesome!", "Python is awesome!", "Python is awesome!",]
for x in python:
print(x)
#prints the entire list
A for loop that prints of the days of the week and where they are numbered
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
for x in range(0, len(days)): #creates a range from 0 to the amount of values in days (7)
print(days[x] , "is Day", x+1, "of the week") #prints the days from the list then "is Day" and adds the value from the range +1 as it starts at 0, "of the week"
Differently worded loop
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
days_suffix = ["st", "nd", "rd", "th", "th", "th", "th"]
for index, suffix in zip(range(0, len(days)), days_suffix):
print(days[index] , "is the", index+1, suffix, "Day of the week")
This loop actually works
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
for x in range(0, len(days)): #creates a range from 0 to the amount of values in days (7)
for y in "1st", "2nd", "3rd", "4th", "5th", "6th", "7th":
print(days[x] , "is the", y, "day of the week")
This one doesn’t as the loop will continue to print monday is 1st, 2nd 3rd, etc. for each day.
Another way of making the loop work much simpler is this:
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
days_number = ["1st", "2nd", "3rd", "4th", "5th", "6th", "7th"]
for x in range(0, len(days)): #creates a range from 0 to the amount of values in days (7)
print(days[x] , "is the", days_number[x], "day of the week")
This loop works by just running through each value in 2 lists, the reason the previous one didn’t work is because it used a nested loop which would run 7 times each time the 1st loop would run, leading to the undesired outcome.
Programming for Animation Devblog
Status | Prototype |
Category | Other |
Author | Pomanchocho |
More posts
- TB1 Week 10Dec 05, 2022
- TB1 Week 9Dec 05, 2022
- TB1 Week 8Dec 05, 2022
- TB1 Week 7Dec 05, 2022
- TB1 Week 6Dec 05, 2022
- TB1 Week 5Dec 05, 2022
- TB1 Week 3Dec 05, 2022
- TB1 Week 2Dec 05, 2022
Leave a comment
Log in with itch.io to leave a comment.