""" Part B: Here is the fixed program """ def divide_and_round(n): """ Divides an integer n by 2 and rounds up to the nearest whole number """ if n % 2 == 0: n = n / 2 else: n = (n + 1) / 2 return n def main(): n = 42 n = divide_and_round(n) print(n)
There are several ways of solving this problem. Here are a few
def main(): # this approach loops 100 times for i in range(100): print(2 * i + 1) # each time i goes up by 1, this term goes up by 2 #-----------------------------------------------------------# def main(): # this approach loops 200 times and looks for odd numbers for i in range(200): if i % 2 == 1: # is i odd? If you divide by 2 is 1 left over? print(i) # if i is odd, print it! #-----------------------------------------------------------# def main(): # this approach uses a while loop value = 1 # this number keeps track of the current oddnum while value < 200: # keep going while its less than 200 print(value) # dont forget to actually print it value += 2 # change the value to increase by 2
""" Write your answer for odd numbers below here: """ def main(): height = float(input("Enter height in meters: ")) if height < 1 or height > 2: print("You can't ride the roller coaster") else: print("You can ride the roller coaster ")
def main(): while left_is_clear(): farm_row() change_row() farm_row() def farm_row(): """ Pick up all the beepers in one row Pre: Karel is facing East at the start of a row Post: Karel is facing East at the end of a farmed row """ move_to_next_beeper_on_row() while beepers_present(): farm_beeper() move_to_next_beeper_on_row() def change_row(): """ Reposition for the next row """ turn_around() move_to_wall() turn_right() move() turn_right() def move_to_next_beeper_on_row(): """ Pre: Karel is facing east on first column Post: Karel is either on the next beeper, or at the end of the row """ safe_move() while no_beepers_present() and front_is_clear(): move() def farm_beeper(): """ Pre: Karel is standing on a beeper to be harvested Post: Karel and the beeper are at the start of the row. Karel is facing East """ pick_beeper() turn_around() move_to_wall() turn_around() put_beeper() # Some common utility functions def safe_move(): if front_is_clear(): move() def move_to_wall(): while front_is_clear(): move() def turn_around(): turn_left() turn_left()
def main(): curr_num = get_num_from_user() while curr_num != 0: if is_perfect(curr_num): print(str(curr_num) + " is perfect!") else: print(str(curr_num) + " is not perfect!") curr_num = get_num_from_user() def get_num_from_user(): return int(input("Your number: ")) def is_perfect(n): # this range gives the values 1, 2, ... , n-1 sum_factors = 0 for i in range(1, n): # i is a factor if it it leaves no remainder if n % i == 0: sum_factors += i return sum_factors == n
def make_ascending(old_list): new_list = [] for next_value in old_list: if is_ascending(new_list, next_value): new_list.append(next_value) return new_list def is_ascending(new_list, next_value): """ Returns true if next_value should be added to the new_list """ if len(new_list) == 0: return True return next_value >= new_list[-1]