Implement the following functions in the given code:
def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n): def is_leap_year(year): def get_letter_grade_version_1(x): def get_letter_grade_version_2(x): def get_letter_grade_version_3(x):
Pay careful attention to the three different versions of the number-grade-to-letter-grade functions. Their behaviors are subtly different.
Use the function descriptions provided in the code to replace the pass keyword with the necessary code.
Remember: Parameters contain values that are passed in by the caller. You will need to make use of the parameters that each function has. Do not change the parameters. Do not change the names of the parameters and do not change the values of the parameters. Just make use of the values given to you.
When you run your program it will load, execute, and finish. Nothing will display unless you write test code outside of the functions.
Read all of the comments in the starter very carefully. All the necessary requirements are written out for you.
Starter code:
#
# I am importing this module so we can use its functions. # import random # Reference: https://docs.python.org/3/library/random.html
# ---------------------------------------------------------
# ---------------------------------------------------------
#
# Poker cards are numbered 1 through 13 (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King)
# and come in four suits: Spades, Hearts, Clubs, Diamonds.
#
# random_suit_number() returns a number 0 <= N < 4. Section 4.5 of your textbook covers this.
#
# get_card_suit_string_from_number(n) takes an integer and returns a string with the name of a suit:
# 0 ---> 'Spades'
# 1 ---> 'Hearts'
# 2 ---> 'Clubs'
# 3 ---> 'Diamonds'
# Note that get_card_suit_string_from_number(n) should return None if n is not a valid value. def random_suit_number(): ''' Returns a random integer N such that 0 <= N < 4. ''' pass def get_card_suit_string_from_number(n): ''' Returns the name of the suit that corresponds to the value n, or None if n is an invalid number. ''' pass
# Expected results:
# get_card_suit_string_from_number(1) ----> 'Hearts'
# get_card_suit_string_from_number(5) ----> None
# get_card_suit_string_from_number(-1) ----> None
#
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
# Poker cards are numbered 1 through 13 (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King)
# and come in four suits: Spades, Hearts, Clubs, Diamonds.
#
# random_value_number() returns a number 1 <= N <= 13. Section 4.5 of your textbook covers this.
#
# get_card_value_string_from_number(n) takes an integer and returns a string with the name of a card value:
# 1 ---> 'Ace' # 2 ---> '2' # 3 ---> '3' # ... # 10 ---> '10' # 11 ---> 'Jack' # 12 ---> 'Queen' # 13 ---> 'King'
# Note that get_card_value_string_from_number(n) should return None if n is not a valid value. def random_value_number(): pass def get_card_value_string_from_number(n): pass
# Expected results: # get_card_value_string_from_number(12) ---> 'Queen'
# get_card_value_string_from_number(7) ---> '7'
# get_card_value_string_from_number(-7) ---> None
# get_card_value_string_from_number(0) ---> None
# get_card_value_string_from_number(14) ---> None
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
#
# The rules for determining whether a year is a leap year (it has # February 29 on the calendar) are a little strange:
# - a year is a leap year if it is a multiple of 4
# - unless it is a multiple of 100, in which case it is NOT a leap year
# - unless it is a multiple of 400, in which case it IS a leap year def is_leap_year(year): ''' Return True if year is a leap year, or False otherwise. ''' pass
#
# Expected results:
# is_leap_year(2000) ----> True
# is_leap_year(2001) ----> False
# is_leap_year(2012) ----> True
# is_leap_year(2040) ----> True
# is_leap_year(2100) ----> False
# is_leap_year(2104) ----> True
# is_leap_year(2200) ----> False
# is_leap_year(2400) ----> True
# is_leap_year(3000) ----> False
#
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
#
# Convert a numerical grade to a letter grade using the following chart:
# A 90 and higher
# B 80 to 89
# C 70 t0 79
# D 60 to 69
# F 0 to 59 def get_letter_grade_version_1(x): ''' Return the letter grade corresponding to the given numerical grade. '''
# Assume all inputs are valid and do not do any extra work. pass
# Expected results:
# get_letter_grade_version_1(85) ----> 'B'
# get_letter_grade_version_1(43) ----> 'F'
# get_letter_grade_version_1(104) ----> 'A'
# get_letter_grade_version_1(79.9) ----> 'C'
# get_letter_grade_version_1(70.3) ----> 'C'
# get_letter_grade_version_1('70') ----> should cause an error
# get_letter_grade_version_1('AB') ----> should cause an error
#
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
#
# Same grade chart as above, but check whether x is an integer.
# If x is not an integer then return None. def get_letter_grade_version_2(x): ''' Return the letter grade corresponding to the given numerical grade, or None if x is not an integer. ''' pass
# Expected results: # get_letter_grade_version_2(85) ----> 'B'
# get_letter_grade_version_2(43) ----> 'F'
# get_letter_grade_version_2(104) ----> 'A'
# get_letter_grade_version_2(79.9) ----> None
# get_letter_grade_version_2(70.3) ----> None
# get_letter_grade_version_2('70') ----> None
# get_letter_grade_version_2('AB') ----> None
# # ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
#
# Same grade chart as above, but do what you can to interpret
# x as a number and to round to the nearest whole number.
# Return None if you cannot interpret x as a number. def get_letter_grade_version_3(x): ''' Return the letter grade corresponding to the given numerical grade, given the instructions. ''' pass
# Expected results:
# get_letter_grade_version_3(85) ----> 'B'
# get_letter_grade_version_3(43) ----> 'F'
# get_letter_grade_version_3(104) ----> 'A'
# get_letter_grade_version_3(79.9) ----> 'B'
# get_letter_grade_version_3(70.3) ----> 'C'
# get_letter_grade_version_3('70') ----> 'C'
# get_letter_grade_version_3('AB') ----> None
# # ---------------------------------------------------------
# ---------------------------------------------------------
Here is the Python code:
import random
def random_suit_number ():
return random.randint (0, 3)
def get_card_suit_string_from_number(n):
if n == 0: return ('Spades')
if n == 1: return ('Hearts')
if n == 2: return ('Clubs')
if n == 3: return ('Diamonds')
return ('None')
def random_value_number():
return random.randint (1, 13)
def get_card_value_string_from_number(n):
if n == 1: return 'Ace'
if n == 11: return 'Jack'
if n == 12: return 'Queen'
if n == 13: return 'King'
if n >= 2 and n <= 10: return str(n)
return ('None')
def is_leap_year(year):
if year % 400 == 0: return True
if year % 100 == 0: return False
if year % 4 == 0: return True
return False
def get_letter_grade_version_1(x):
if x >= 90: return 'A'
if x >= 80: return 'B'
if x >= 70: return 'C'
if x >= 60: return 'D'
return 'F'
def get_letter_grade_version_2(x):
if not isinstance (x, int) and not isinstance (x, float) : return 'None'
return get_letter_grade_version_1(x)
def get_letter_grade_version_3(x):
try:
x = float(x)
except ValueError:
return 'None'
return get_letter_grade_version_2(x)
Get Answers For Free
Most questions answered within 1 hours.