Need help writing these functions in Python:
FUNCTION 1
get_course_ids: Consumes a list of Course dictionaries and returns a list of integers representing course IDs.
Here's what I have so far but I'm getting a Type Error - list indices must be integers or slices, not dict.
def get_course_ids(courses):
course_ids = []
for course in courses:
course_ids.extend(courses[course])
return course_ids
FUNCTION 2
choose_course: Consumes a list of integers representing course IDs and prompts the user to enter a valid ID, and then returns an integer representing the user's chosen course ID. If the user does not enter a valid ID, the function repeatedly loops until they type in a valid ID. You will need to use the input function to get the user's choice.
Funtion1 :
def get_course_ids(courses):
course_ids = []
for course in courses:
course_ids.extend(courses[course])
return course_ids
the above one is your code.
Observe the for loop which is indeed a for each loop.
So inorder to acces each element, it is not necessary to use list
name with indexing again.
course_ids.extend(courses[course]) -- this is bug line
replace tha above line with below line
course_ids.extend(course) --- course itself is a single value in the list
the modified function will look like this ---
def get_course_ids(courses):
course_ids = []
for course in courses:
course_ids.extend(course)
return course_ids
Funtion 2 :
# returns an integer representation of the given course id
# please enter digits as input
# on giving non-digits as input, this program may give error
def choose_course(courses):
# taking input from user
cid = int(input("Enter a course ID : "))
while cid not in courses ;
# ask input until user enters a
valid input
print("Wrong input")
cid = int(input("Enter a course ID
: "))
# I'm not sure what is integer representation of
the Course ID as Course ID itself is a integer
# so I'm returning index of the given course ID
# If you feel this is incorrect you can change and
return your required value
re = courses.index(cid)
return re
Need help writing these functions in Python: FUNCTION 1 get_course_ids: Consumes a list of Course dictionaries...
Python Question: Define the function high_score that consumes a list of integers (representing scores in a game) and produces an integer representing the highest score in the list. Ignore scores less than 100, and stop processing values if you encounter -999. If the list is empty, return the value None instead. It is up to you to decompose this function (or not) however you want. Here is my code so far: from cisc108 import assert_equal def high_score(scores: [int])->int: max_num =...
Using PYTHON: (Find the index of the smallest element) Write a function that returns the index of the smallest element in a list of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: def indexOfSmallestElement(lst): Write a test program that prompts the user to enter a list of numbers, invokes this function to return the index of the smallest element, and displays the index. PLEASE USE LISTS!
Python GPA calculator: Assume the user has a bunch of courses they took, and need to calculate the overall GPA. We will need to get the grade and number of units for each course. It is not ideal to ask how many courses they took, as they have to manually count their courses. Programming is about automation and not manual work. We will create a textual menu that shows 3 choices. 1. Input class grade and number of units. 2....
Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================") print(" Baseball Team Manager") print("MENU OPTIONS") print("1 - Calculate batting average") print("2 - Exit program") print("=====================================================================") def convert_bat(): option = int(input("Menu option: ")) while option!=2: if option ==1: print("Calculate batting average...") num_at_bats = int(input("Enter official number of at bats: ")) num_hits = int(input("Enter number of hits: ")) average = num_hits/num_at_bats print("batting average: ", average) elif option !=1 and option !=2: print("Not a valid...
GPA calculator: Assume the user has a bunch of courses they took, and need to calculate the overall GPA. We will need to get the grade and number of units for each course. It is not ideal to ask how many courses they took, as they have to manually count their courses. Programming is about automation and not manual work. We will create a textual menu that shows 3 choices. 1. Enter course grade and units. 2. Show GPA. 3....
PYTHON PROGRAMMING NEED HELP ASAP You will write an application to manage music collections -- a music collection is a list of albums. The named tuples used for Albums and Songs are defined below, and an example of a music collection is given. (DO NOT HARDCODE THE VALUES FOR MUSIC!) Album = namedtuple('Album', 'id artist title year songs') Song = namedtuple('Song', 'track title length play_count') MUSIC = [ Album("1", "Peter Gabriel", "Up", 2002, [Song(1, "Darkness", 411, 5), Song(2, "Growing Up",...
Solve the code below: CODE: """ Code for handling sessions in our web application """ from bottle import request, response import uuid import json import model import dbschema COOKIE_NAME = 'session' def get_or_create_session(db): """Get the current sessionid either from a cookie in the current request or by creating a new session if none are present. If a new session is created, a cookie is set in the response. Returns the session key (string) """ def add_to_cart(db, itemid, quantity): """Add an...
urgent Help needed in python program ! Thanx # This is a function definition. You can ignore this for now. def parse_integer_list_from_string(string): """ Returns a list of integers from a string containing integers separated by spaces. """ # Split the line into a list of strings, each containing a number. number_string_list = string.split() # Create an empty list to store the numbers as integers. numbers = [] # Convert each string to an integer and add it to the list...
C++ C. In the file c_final_practice.cpp: (1) Write a function void replace(vector<int>& v, int old, int new) that replaces all occurrences of the integer old in v with the integer new. For example, if v is the vector {1,2,1,3}, then after calling replace(v, 1, 3) v should be {3,2,3,3}. (2) Write the main-function so that it prompts the user for a list of integers, and then uses your replace function to display the following lists: • The user's list with...
Write a Python program that tests the function main and the functions discussed in parts a through g. Create the following lists: inStock - 2D list (row size:10, column size:4) alpha - 1D list with 20 elements. beta - 1D list with 20 elements. gamma = [11, 13, 15, 17] delta = [3, 5, 2, 6, 10, 9, 7, 11, 1, 8] a. Write the definition of the function setZero that initializes any one-dimensional list to 0 (alpha and beta)....