Wk 10 Ch 5B - Functions Practice with Simple Game

docx

School

Lone Star College System, Woodlands *

*We aren’t endorsed by this school

Course

1315

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

8

Uploaded by MateBat2484

Report
Name: Adrian Diaz Week 10 Chapter 5 Part B — Functions | Simple Game Functions Practice - Part B Part 1-A: Study the code below, then answer the questions CODE: def printHeader (): # print the header for the detail data print(format("Name Average")) def printResults (name, avg): # print detail data print(format(name,"20"), format(avg, "5.1f")) def calcAverageResults (t1, t2, t3): # calculate average and print results avg = float((t1+t2+t3)/3) return avg def main(): name = input("Enter name: ") tst1 = int(input("Enter grade 1: ") tst2 = int(input("Enter grade 2: ") tst3 = int(input("Enter grade 3: ") printHeader() avg = float(calcAverageResults(tst1, tst2, tst3)) printResults(name, avg) main() QUESTIONS: 1. What 4 things do all function have? Def, name,( ), : 2. Which function has a return value? calcAverageResults 3. Which function has the most parameters? How many parameters? calcAverageResults. 3 parameters 4. Which function has input statements? How many input statements? Main. 4 input statements 5. Which function has no parameters? printHeader 6. What function is called from within printHeader()? print 7. What is the first function called in main? name 1
Name: Adrian Diaz Week 10 Chapter 5 Part B — Functions | Simple Game Part 2-A: Study the code below, then answer the questions CODE: def main(): #Step 0: Initialize upperPressure = int(0) lowerPressure = int(0) #Step 1: Input upperPressure = int(input("Enter your blood pressure upper number: ")) lowerPressure = int(input("Enter your blood pressure lower number: ")) #Step 3: Process and Output printOutput(upperPressure, lowerPressure) def getBPMessage(upperP, lowerP): msg = str("") if upperP < 120 and lowerP < 80: msg = "you have normal blood pressure" elif upperP < 130 and lowerP < 80: msg = "you have elevated blood pressure" elif upperP < 140 or lowerP >= 80 and lowerP <= 89 : msg = "you have high blood pressure (hypertension) Stage 1" elif upperP <= 180 or lowerP >= 90 and lowerP <= 120: msg = "you have high blood pressure (hypertension) Stage 2" elif upperP > 180 or lowerP > 120: msg = "you are in a hypertensive crisis. Contact your doctor immediately)" return msg def printOutput(upperP, lowerP): print("\n\nWelcome to the Blood Pressure Evaluator 2020\n") print("Your blood pressure results are:") print(" Systolic Pressure:",upperP) print(" Diastolic Pressure:",lowerP) print("\n Blood pressure of",str(upperP)+"/"+str(lowerP),\ "indicates that", getBPMessage(upperP, lowerP)) main() QUESTIONS: 1. List the names of the user-defined functions: getBPMessage, printOutput,main 2. List the names of the python-provided functions used in the program: Input,print,in,str 3. Which user-defined function has a return value? getBPMessage 4. Which function calls the getBPMessage() function? PrintOutput 5. Which function calls the printOutput() function? 2
Name: Adrian Diaz Week 10 Chapter 5 Part B — Functions | Simple Game printOutput 6. Which function processes the blood pressure values to determine the result? getBPMessage 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Name: Adrian Diaz Week 10 Chapter 5 Part B — Functions | Simple Game Part 3: Simple Game Program Worth 25 points Description: You want to create a game for 2 people that uses a single dice roll to move you around the 20 space board. However, as the player goes from start to finish, occasionally they will be given a "chance" opportunity: player is asked to guess whether or not the roll is even or odd. If player is correct, they move forward the original spaces rolled. If they fail, they move back the number of spaces rolled. The first player to reach the end, wins. Requirements: · Standard Requirements: Includes comments for each section of code specifying input, process, and output for each: Initialize all variables prior to usage Use constants instead of literals: MAX_SPACES = 20, etc. Code separated into IPO sections, and appropriate comments. NOTE : Do not use any Python programming techniques that we have not yet covered in class. This includes special operators/functions, lists, arrays, and tuples: basically cool shortcuts that replace techniques we have been using in class, (Not because the cool shortcut is not useful and time-saving, but because I need to see that you understand the underlying concepts.) · Use functions to: Ask for players names Roll dice to determine spaces; if dice value is evenly divisible by 3, player should perform "chance" task Perform even/odd task: player guesses even or odd; dice is rolled. If result matches guess, return a value that indicates player should move forward the original number of spaces rolled. Otherwise, return value indicating player should move the original number of spaces rolled backward. Print result of each play for each each player. (See sample output) 4
Name: Adrian Diaz Week 10 Chapter 5 Part B — Functions | Simple Game Sample Input/Output: 5 Enter name of player 1: Joe Enter name of player 2: Sue Round 1: Joe rolls the dice and gets 4 Joe moves 4 spaces forward and is now on space 4 Sue rolls the dice and gets 5 Sue moves 5 spaces forward and is now on space 5 Round 2: Joe rolls the dice and gets 2 Joe moves 2 spaces forward and is now on space 6 Sue rolls the dice and gets 3 Chance Opportunity: Guess whether the next roll of the dice will be odd or even: even The dice roll is 5 which is odd Sue goes back 3 spaces and is now on space 2 Round 3: Joe rolls the dice and gets 5 Joe moves 2 spaces forward and is now on space 11 Sue rolls the dice and gets 4 Sue moves 4 spaces and is now on space 6 Round 4: Joe rolls the dice and gets 6 Chance Opportunity: Guess whether the next roll of the dice will be odd or even: odd The dice roll is 1 which is odd Joe moves 6 spaces forward and is now on space 17 Sue rolls the dice and gets 4 Sue moves 4 spaces and is now on space 10
Name: Adrian Diaz Week 10 Chapter 5 Part B — Functions | Simple Game Insert Copy/Pasted Source Code: #Adrian Diaz # Simple Game Program Bonus import random MAX_SPACES = int(20) def main(): #Initialize player1Nm = str("") player2Nm = str("") player1Space = int(0) oldSpace1 = int(0) player2Space = int(0) oldSpace2 = int(0) roundNum = int(0) player1Roll = int(0) player2Roll = int(0) #Input player1Nm = getPlayerName(1) player2Nm = getPlayerName(2) #Process print("Welcome to the game",player1Nm,"and",player2Nm) 6 Round 5: Joe rolls the dice and gets 4 Joe moves 4 spaces forward and is now on space 21 Sue rolls the dice and gets 5 Sue moves 5 spaces and is now on space 15 End of Game! Joe wins on space 21 Sue is on space 15
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Name: Adrian Diaz Week 10 Chapter 5 Part B — Functions | Simple Game while player1Space < MAX_SPACES and player2Space < MAX_SPACES: roundNum += 1 print("\nRound:",roundNum) player1Roll = getPlayerRoll() oldSpace1 = player1Space player1Space += player1Roll printResult(player1Nm,oldSpace1,player1Space,player1Roll) player2Roll = getPlayerRoll() oldSpace2 = player2Space player2Space += player2Roll printResult(player2Nm,oldSpace2,player2Space,player2Roll) printFinalResult(player1Nm,player1Space,player2Nm,player2Space) def getPlayerName(num): name = str(input("Enter player name " + str(num)+": ")) return name def getPlayerRoll(): roll = int(random.randint(1,6)) return roll def printResult(playerName,oldSpace,newSpace,roll): print(playerName,"is on",oldSpace,"and rolls",roll) print(playerName,"moves",roll,"spaces forward and is now on space",newSpace) print("")#empty space def printFinalResult(player1,player1Space,player2,player2Space): print("\nEnd of Game!") if player1Space > player2Space: print(player1,"wins on space",player1Space,"and wins the game!") print(player2,"wins on space",player2Space) elif player1Space < player2Space: print(player1,"is on space",player1Space) print(player2,"is on space",player2Space,"and wins the game!") else: print(player1,"and",player2,"tied!") main() 7
Name: Adrian Diaz Week 10 Chapter 5 Part B — Functions | Simple Game Insert Output Screenshot Image (of the last two turns of the game): 8