for
loopwhile
loopin
(and not in
) operator used on lists and stringsrandrange(start, stop)
, choice(<sequence>)
Write a function called getDigit() that asks the user for a numerical digit and returns the given digit. Your function should check to make sure the user enters just a single digit. If the user enters anything other than a single digit, the function should ask again, until it receives valid input.
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. For example, maxList([7,4,8,1]) should return 8. Write a main program to test the function.
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.