repetition
indented means inside for loop will be carried out
furits = ["Apple", "Peach", "Pear"]
for fruit in fruits:
print(fruit)
Exercise 5.1 Average Height `
Naming item in for loop: Giving singular form of item is recommended rather than using "i" or meaningless name. As it can be confusing. so refer to it as height, number.
# 🚨 Don't change the code below 👇
student_heights = input("Input a list of student heights ").split()
for n in range(0, len(student_heights)):
student_heights[n] = int(student_heights[n])
# 🚨 Don't change the code above 👆
#Write your code below this row 👇
sum = 0
for i in student_heights: #i -> height
sum += i
#print(sum)
num_student = 0
for i in student_heights: #i -> number
num_student += 1
#print(num_student)
avg_height = round(sum/num_student)
print(avg_height)
Exercise 5.2 Highest Score
I was not able to solve the problem... logic behind was really simple. Using variable to store the data, was the key. I can make an empty variable before loop, and store the data as it goes into a loop.
# 🚨 Don't change the code below 👇
student_scores = input("Input a list of student scores ").split()
for n in range(0, len(student_scores)):
student_scores[n] = int(student_scores[n])
print(student_scores)
# 🚨 Don't change the code above 👆
#Write your code below this row 👇
#print(max(student_score))
#mycode that didn't work
student_scores = [98, 12, 87]
for n in range(0, len(student_scores)):
if (student_scores[n-1] > student_scores[n]):
print(student_scores[n-1])
else:
print(student_scores[n])
#Angela's code
highest_score = 0
for score in student_scores:
if score > highest_score:
highest_score = score
print(f"The highest score in the class is: {highest_score}")
range(start, stop, step)
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.
last digit needs to be +1 of the number you actually want.
#Write your code below this row 👇
# approach1
total = 0
for number in range(1, 101):
if number % 2 == 0:
total += number
print(total)
# approach2
total = 0
for number in range(2, 101, 2):
total += number
print(total)
Order in logic was important in this code.
#Your program should print each number from 1 to 100 in turn.
#When the number is divisible by 3 then instead of printing the number it should print "Fizz".
#`When the number is divisible by 5, then instead of printing the number it should print "Buzz".`
#`And if the number is divisible by both 3 and 5 e.g. 15 then instead of the number it should print "FizzBuzz"`
for number in range(1, 101):
if (number % 3) == 0 and (number % 5) == 0:
print("FizzBuzz")
elif number % 3 == 0:
print("Fizz")
elif number % 5 == 0:
print("Buzz")
else:
print(number)
This task was quite difficult. I was overwelmed. I continued and persevered and it started clicking... I manage to solve it in my own way.
For the for loop, I need more practice to get my head around it.
Range was another tricky one. I was confused it with index, stating it it from 0 but in this case, I needed it to start with 1 as it was counting numbers.
#Password Generator Project
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
#mycode - Order not randomised :( ----------------------------------------------------------------
#Eazy Level
#e.g. 4 letter, 2 symbol, 2 number = JduE&!91
i = 0
password = ' '
for number in letters:
if i < nr_letters:
password += number
i += 1
i = 0
for number in numbers:
if i < nr_numbers:
password += number
i += 1
i = 0
for number in symbols:
if i < nr_symbols:
password += number
i += 1
print(password)
#mycode - Order not randomised :( ----------------------------------------------------------------
#Hard Level - Order of characters randomised:
#e.g. 4 letter, 2 symbol, 2 number = g^2jk8&P
i = 0
random_password = []
for char in letters:
if i < nr_letters:
random_password += number
i += 1
i = 0
for char in numbers:
if i < nr_numbers:
random_password += number
i += 1
i = 0
for char in symbols:
if i < nr_symbols:
random_password += number
i += 1
#Angel's solution ----------------------------------------------------------------
#easy
password = ""
for char in range(1, nr_letters + 1):
password += random.choice(letters)
for char in range(1, nr_symbols + 1):
password += random.choice(symbols)
for char in range(1, nr_numbers + 1):
password += random.choice(numbers)
print(password)
#hard
password = []
for char in range(1, nr_letters + 1):
password += random.choice(letters)
for char in range(1, nr_symbols + 1):
password += random.choice(symbols)
for char in range(1, nr_numbers + 1):
password += random.choice(numbers)
print(password)
random.shuffle(password)
print(password)
final = ''
for char in password:
final += char
print(final)
#my 2nd code using sample rather than shuffle ----------------------------------------------------------------
digit = nr_numbers + nr_symbols + nr_letters
sampling = random.sample(password, digit)
hard_password = ""
for char in sampling:
hard_password += char
print(hard_password)