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
def main():
n = 42
divide_and_round(n)
print(n) # should print 21
divide_and_round
and
main
, but divide_and_round must still implement the
functionality described in its comment for all possible values of its
parameter. You should write a fixed version of the program with
comments indicating each line you changed.
Write a program that prints the first 100 odd numbers greater than 0. In other words print out 1 then 3, then 5 all the way up to, and including, 199.
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199
Write a main function which asks the user for their height in meters.
If their height is less than 1 meter or greater than 2 meters, print "You can't ride the roller coaster".
If their height is between 1 meter and 2 meters print "You can ride the roller coaster".
Here are a few examples. User input is in blue
Enter height in meters: 1.2
You can ride the roller coaster.
Enter height in meters: 2.5
You can't ride the roller coaster.
Karel has recently started a new job as a farmer, and needs you to write a program called FarmerKarel to help it gather up crops (represented as beepers, of course). Karel starts off in a world of any size, with crops (beepers) scattered around this world. Your program should have Karel, for each row in this world, gather up the beepers in that row and place them on the leftmost square of that row. Here is a before-and-after example:
Note that, in each row, Karel has gathered all beepers in that row and placed them on the leftmost square. If there are no beepers in a row, Karel should not place any beepers in that row
You may assume the following facts about the world:
and
, or
, not
.
Write a program that reads in integers one by one from the user and prints out whether those integers are perfect. A perfect number is a number whose factors sum is the same as itself. For example, 28 is a perfect number, since its factors (1, 2, 4, 7 and 14) sum to 28. On the other hand, 42’s factors (1, 2, 3, 6, 7, 14, 21) sum to 58, and so it isn’t a perfect number. Your program should print an error message if the number entered is negative, and should end when the user types 0. A sample run of the program is below. User input is in blue
Your number: -12 Please enter a positive number Your number: 28 28 is perfect! Your number: 42 42 is not perfect! Your number: 0
Write a list function
def make_ascending(old_list):
which returns a new list, which has all ascending elements in a list of numbers. An element is ascending, if it is greater-that-or-equal-to the number before it. The first element should always be included in the new list.
Here are a few examples:
make_ascending([1, 2, 3])
should return [1, 2, 3]
make_ascending([1, 2, 3, 2, 5])
should return [1, 2, 3, 5]
make_ascending([-1, 3, 5])
should return [-1, 3, 5]
make_ascending([5, 3, 1])
should return [5]
make_ascending([])
should return []