Week 2
///2.Bugs
print("Hi","there,","how are you?")
print("Hi", "there," , "how are you?")
and
print("Hi","there,","how are you?")
print("Hi","there, ","how are you?")
Why do the first two lines produce the same output, while the last two result in different outputs?
That is because whatever is inside the parenthesis also needs to be inside quotation marks to be shown. The second line the comma and spaces will be omitted since they are not in quotation marks. In the 4th line example the comma and spaces are within the quotation marks, that's why the result will be different from the 3rd line.
///4.Comments
#this is a comment
#comments can be used to deactivate a piece of scripting
Commenting helps us understand the code in a script better since abbreviations and scripting language is used. We could also use comments to leave out a portion of a scipt for debugging reasons. If the commented part is the reason that stopped the code from producing a desired result then we know that this is the part that we need to work on.
///5.Math operators
print(10 / 2)
# The operator / divides two numbers
#This will output 5
print("Roosters", 100 - 25 * 3 % 4)
#The operator * is used to caclulate the multiplication of two numbers
#So the result of this is going to be: Roosters, 97
print(3 + 2 < 5 - 7)
#The > and < operator are used to compare two values. The output from this comparison is either going to be True or False. This comparison is False because 5<7
print("Is it greater or equal?", 5 >= - 2)
The <= and >= are used to indicate if something is equal or lesser and equal or bigger. So in this case it is False because 5>-2. Only one of the two statements (<> and =) needs to be True
/// 6. Script that creates a 5x5 cube made of "@"'s:
i=1
while i < 5:
i += 1
print("@@@@@")
#The result of this will be
#@@@@@
#@@@@@
#@@@@@
#@@@@@
#@@@@@
///Script that creates a christmas tree
import maya.cmds as cmds
for i in range(1,10,2):
print(('*'*i).center(10)) # the .center(_) is used to centre the asteriscs of the tree. We make the tree by multiplying by i everytime
for y in range(2):
print(('||').center(10))
///7. Calculating the temperature from F to C
#when converting for example 25 degrees C to F, we do this as follows: print(25*1.8+32)
print(30*1.8+32,"degrees Fahrenheit") #Converts 30 degrees Celcius to Fahrenheit
print(45*1.8+32,"degrees Fahrenheit") #Converts 45 degrees Celcius to Fahrenheit
/// 8. Variables
This code calculates what is the average number of passengers per car
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven # divides the overall passengers with the overall number of cars driven
///Calculating module marks
#a)the sum of the marks for all modules in year 1
#year 1
module1 = 56
module2 = 71
module3 = 67
module4 = 70
module5 = 76
module6 = 60
sum_of_modules = ( module1 + module2 + module3 + module4 + module5 + module6 )
print(sum_of_modules)
#b) 25% of the overall mark achieved in modules two and three;
print((module2 +module3)*(25/100))
#c) the average overall mark achieved across all four modules in year 1 (hint: add all marks together and divide by 4);
print ((module1 + module2 + module3 + module4)/4)
#d)the weighted average overall mark achieved across all modules in year 1, assuming that units one, two and three are worth 20% of the final year mark each, while unit four is worth 40% of the final mark
print ( (module1 + module2 + module3)*(20/100) + (module4 + module5 + module6)*(40/100))
///what is the difference between = (single-equal) and == (double- equal) in Python scripting?
A single equal mark is used to assign a value to a variable, whereas two consecutive equal marks is used to check whether 2 expressions give the same value
Leave a comment
Log in with itch.io to leave a comment.