Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to understand how to write a test code for the following (including the roll function by using a seed). Tried giving it a

I need to understand how to write a test code for the following (including the roll function by using a seed). Tried giving it a go with pytest and couldn't get anything to work. import random def main(): '''Main function to initiate play'''  computer_score = 0 human_score = 0 instructions() while is_game_over(computer_score, human_score) == False: computer_score += computer_move(computer_score, human_score) human_score += human_move(computer_score, human_score) show_results(computer_score, human_score) def instructions(): '''Welcome message and rules of game'''  print('Welcome to "Hundred"') print('You will take turns with the computer to roll the die.') print('Each number you roll will be added to your score.') print('If you roll a 1, your turn ends and you receive no points for that round.') print('First to 100 wins the game. Computer goes first.') def human_move(computer_score, human_score): '''Initiates human's turn and returns their score'''  show_results(computer_score, human_score) roll_value = roll() if roll_value == 1: turn_score = 0 print('First roll was a 1. Your turn has ended.') return turn_score else: turn_score = roll_value print('Your turn!') print('You rolled: ', roll_value,) while ask_yes_or_no('Roll again? Yes/No.'): roll() if roll_value != 1: print('You rolled: ', roll_value,) turn_score += roll_value else: turn_score = 0 print('Your turn has ended. No points this round.') return turn_score print('Points this round: ', turn_score) return turn_score human_score += turn_score if computer_score > human_score: print(' You are behind by '+ str(computer_score - human_score) + ' points.') elif human_score > computer_score: print(' You are ahead by ' + str(human_score - computer_score) + ' points.') return human_score def computer_move(computer_score, human_score): '''initialises turn_score and computer's turn. Limits computers number of rolls with a maximum roll score of 20'''  show_results(computer_score, human_score) turn_score = 0 roll_value = 0 print('Computer\'s turn!') while turn_score < 20 and computer_score < 100: roll_value = roll() print('Computer rolled: ', roll_value) if roll_value != 1: turn_score += roll_value else: turn_score = 0 print('Turn has ended.') return turn_score print('Points this round: ', turn_score) computer_score += turn_score return computer_score def is_game_over(computer_score, human_score): '''checks scores against win criteria after each move human makes'''  if computer_score == human_score: return False if human_score >= 100 or computer_score >=100: print(' Final Scores Computer: ' + str(computer_score) + ' Your Score: ' + str(human_score)) return True else: return False def roll(): '''Generates value of rolled die'''  return random.randint(1, 6) def ask_yes_or_no(prompt): '''checks if player wants to roll again'''  while True: ans = input('Roll again? Yes/No. ') if ans[0] == 'y' or ans[0] == 'Y': return True elif ans[0] == 'n' or ans[0] == 'N': print('Your turn has ended') return False else: print("Invalid response. Please enter Yes or No: ") def show_results(computer_score, human_score): '''prints computer and human results'''  print(' Computer\'s score: ' +str(computer_score)) print('Your Score: ' + str(human_score), ' ') if is_game_over(computer_score, human_score): if human_score > computer_score: print('You have won by ' + str(human_score - computer_score) + ' points.') elif computer_score > human_score: print('Computer wins by '+ str(computer_score - human_score) + ' points.') #Starts the program if __name__ == '__main__': main() 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

More Books

Students also viewed these Databases questions

Question

Question What integration level should an employer choose?

Answered: 1 week ago