Lab_04

txt

School

Toronto Metropolitan University *

*We aren’t endorsed by this school

Course

213

Subject

Electrical Engineering

Date

Jan 9, 2024

Type

txt

Pages

3

Uploaded by GeneralSteel9352

Report
# CPS109_Fall2022_Lab04 # Name: Ahmed Dhillon # Student ID: 501176485 def question_1(): # Setting a counter for the sum of even numbers sum_of_even = 0 # This for loop will iterate over every number from 0, 100 # The range method is not inclusive, so I have to set the range 1 higher than the amount wanted for i in range(101): # In the for loop it checks if the number is even if i % 2 == 0: # If the number is even it adds it to the counter sum_of_even = sum_of_even + i # After the loop is finished iterating through all 100 it will print the sum print(sum_of_even) return() def question_2(): # Setting a counter counter = 0 # For loop will iterate over numbers 1 to 100 for i in range(101): # For every number it will square it and add it to the counter counter += i * i # After the loop it will print the sum if all the numbers squared print(counter) return () def question_3(): for i in range(21): # For every number from 0 to 20 it will square the number and print it print(i**2) return() def question_4(): # Setting a counter counter = 0 # Asking user for 2 numbers a = int(input('enter number: ')) b = int(input('enter number: ')) # This loop will iterate over every number from a to b +1 since range method is not inclusive for i in range(a, b+1): if i % 2 != 0: counter += i print(counter) return() def question_5(): counter = 0 # Asking user for number but keeping it as a string number = (input('enter number: ')) # Using for loop to iterate over every item in the string
for items in number: # Converts items to an integer and checks if it is odd if int(items) % 2 != 0: # If item is odd it adds it to the counter counter += int(items) # Prints the final sum print(counter) return() def question_6(): # Empty list for all numbers list_ = [] # Empty list for all odd numbers odd_list = [] max_num = 0 # For loop to ask user for a number while appending it to the list for i in range(10): list_.append(int(input("enter 10 nums: "))) # For loop iterates over each number in the list and adds it to the odd number list for i in range(len(list_)): if list_[i] % 2 != 0: odd_list.append(list_[i]) # This for loop checks every number in the odd list and finds the max and sets it to the new max num using max method for i in range(len(odd_list)): max_num = max(max_num, odd_list[i]) # checks if the length of the odd list is 0 which means there are no odd numbers and prints no odd numbers if len(odd_list) == 0: print("there are no odd numbers") # Otherwise it prints max num else: print(max_num) return() def question_7(): # counter counter = 0 # empty list for the numbers found in the string digit_list = [] input_string = (input('enter a string: ')) # For loop used to iterate over every character which then checks if it is a number and adds to digit list for i in range(len(input_string)): if input_string[i].isdigit(): digit_list.append(int(input_string[i])) # for loop used to add up every number in the digit list using the counter for nums in digit_list: counter += nums print(counter) return() def question_8(): input_string = (input('enter numbers: ')) counter = 0
# Using .split method to split the string where there are commas and storing in a list digit_list = input_string.split(",") # for loop to add all float number using counter for nums in digit_list: counter += float(nums) print(counter) return() if __name__ == "__main__": question_1() question_2() question_3() question_4() question_5() question_6() question_7() question_8()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help