Scientific Computing
Create a file called activity12.py in cs100/ch2. As you work through the following problems, pay careful attention to whether a function should print something or instead return something. Notice how that effects how a function is called.
factorial that takes one parameter (a nonnegative integer). The function will return the factorial of the number. Call your function to test it. For example,
result = factorial(5)
print(result) # it should print 120
isEven that takes one parameter (a nonnegative integer). The function will return the boolean value True if the given number is even, otherwise, it will return the boolean value False. Call your function to test it. For example,
isFiveEven = isEven(5)
print(isFiveEven) # it should print False
displayStudentInfo which accepts two parameters: a string representing a student’s name and a nonnegative integer representing the student’s age. The function should print a sentence about the student. For example, calling your function to test it will look as follows:
displayStudentInfo("Emily", 20) # it should print "Emily is a 20 year old student"
displayStudentInfo("Bob", 24) # it should print "Bob is a 24 year old student"
multiPrint which accepts two parameters: a string and a nonnegative integer representing the number of times that the string should be printed (hint: use a for loop). Call your function to test it. For example, calling multiPrint("repeat", 5) will print to the Shell the following:
repeat
repeat
repeat
repeat
repeat
getGrade which accepts one parameter: a nonnegative integer score between 0 and 100. The function should return the string “A”, “B”, “C”, “D”, or “F” using the standard grading scale. Call your function to test it, for example:
myGrade = getGrade(89)
print(myGrade) # should print B
positivity which accepts one parameter: an integer n. The function should print either “positive number”, “zero”, or “negative number” depending on the value of n. Call your function to test it. For example,
positivity(5) # should print "positive number"
positivity(-10) # should print "negative number"
Write a function getRandomNumber13 that takes no parameters and returns a random number between 1 and 13 using only functions we have discussed so far. Hint: Use the random() function from the random module, which returns a number in the range [0,1), and multiply it by 13 to get a decimal value x in the range [0.0, 13.0). Next, return the ceiling of x.
Write a function getRandomNumber4 that takes no parameters and returns a random number between 1 and 4. Hint: use the same idea as above.
Write a function getRandomCard which will return a string consisting of a random card in a standard deck of 52 cards. For example, it might return “4 of clubs” or “Ace of diamonds”. Hint: use your getRandomNumber13 and getRandomNumber4 to determine the face value and suit. Call your function to test.
Play this interactive text-based game. Notice how there’s a limited set of inputs that the user can type. Depending on what the user types, something happens. Think about how you might make an interactive story of your own!
Submit your activity12.py file to Moodle.