for
loopwhile
loopin
(and not in
) operator used on lists and stringsrandrange(start, stop)
, choice(<sequence>)
Write a program which asks the user for 4 words and then prints them in reverse order.
Write a function called avgList that, given a list of numbers, calculates and returns their average. For example, avgList([5, 10, 5, 4]) should return 6. Write a main program to test the function.
Write a function called maxList that, given a list of numbers, finds and returns the largest number in the list (without using the built-in max(..)
function). For example, maxList([7,4,8,1]) should return 8. Write a main program to test the function.
Write a program which generates random integers (in the range 0 to 1000) until it encounters a number that is divisible by 3.
Consider the following program.
def main():
print("in main")
test = "whee"
check = "e"
answer = helper(test, check)
print("the answer is %d" % (answer))
def helper(word, letter):
print("in helper")
print("word: %s" % (word))
print("letter: %s" % (letter))
x = 0
for i in range(len(word)):
if word[i] == letter:
x = x+1
# draw stack here, just before return
return x
main()
-1
. After reading in the -1
, the program should compute and display the average grade. Please enter your grades below.
Enter a -1 when you are all done...
grade 1: 98
grade 2: 87
grade 3: 65
grade 4: 95
grade 5: 80
grade 6: -1
The average of those 5 grades is 85.000
results(np, nc)
that takes two parameters, the number of problems, and the number correct. The function should print an appropriate message to the user, based on the percentage the user got correct. For example, if they got 100% correct, print "Super!"; if they got 80% correct, print "Good job."; and so on. Here's a few examples:results(10,10)
: You got 10 out of 10 correct. Super!results(5,1)
: You got 1 out of 5 correct. Try again...results(8,6)
: You got 6 out of 8 correct. Not bad.m = ["t","i","m","e"]
s = "time"
m[1] = "a"
s[1] = "a"
What happens when this code is executed? What are the values of m
and s
afterward?
def double(lst):
total = 0
for i in range(len(lst)):
lst[i] = lst[i] * 2
total += lst[i]
# Q1: What is the function stack here?
return total
def main():
values = [-3, 4, 8, -5]
print(values)
test = double(values)
print("Sum of doubles:", test)
print(values)
# Q2: What is the function stack here?
main()
Use Python Tutor to check your answer! (Keeping in mind that Python tutor does not order the functions on the stack correctly, and that values should all be on the heap.)
def swap(i, j, lst):
temp = lst[i]
lst[i] = lst[j]
# Q1: What is the stack here?
lst[j] = temp
def mystery(lst):
firstIdx = 0
lastIdx = len(lst)-1
while firstIdx < lastIdx:
swap(lastIdx, firstIdx, lst)
lastIdx = lastIdx - 1
firstIdx = firstIdx + 1
def main():
wordList = ["whose", "woods", "these", "are"]
print("before:", wordList)
mystery(wordList)
print("after:", wordList)
main()
Use Python Tutor to check your answer! (Keeping in mind that Python tutor does not order the functions on the stack correctly, and that values should all be on the heap.)