Question: In python, write the code from the Rock, Paper, Scissors Game and the For Loops Guessing Game in a way that uses modules. gather your
In python,
write the code from the Rock, Paper, Scissors Game and the For Loops Guessing Game in a way that uses modules.
gather your modules from Creating Modules: Creating a Sequence Using Recursion and Creating Modules: Work and Power
create a program that imports all of the modules created for (1) and (2) in a way that allows the user to decide which "work" or "pleasure" program to run, and allow them to decide if they would like to run anything else or close the program( ie use a sentinel loop)
You can put all of your functions into one module or create separate modules for each program.
existing code:
import all_functions as A from random import randint cont0="Y" while cont0=="Y": choice=input ("Would you like to do work or play a game? Press W for work and P for play") choice=choice.upper() if choice == "P": gamechoice=input("Would you like to play Rock Paper Scissors or Guessing Game? Press RPS for Rock Paper Scissors and GG for Guessing Game") gamechoice=gamechoice.upper() if gamechoice=="RPS": #Rock Paper Scissor code that calls its module and functions to run from random import randint
def play_game(): choices = ["rock", "paper", "scissors"] cmptr_choice = choices[randint(0, 2)] user_choice = input("Choose rock, paper, or scissors: ").lower() while user_choice not in choices: user_choice = input("Invalid choice. Choose rock, paper, or scissors: ").lower() print("The Computer chose: ", cmptr_choice) if user_choice == cmptr_choice: print("It's a tie! Play again.") return play_game() elif (user_choice == "rock" and cmptr_choice == "scissors") or \ (user_choice == "paper" and cmptr_choice == "rock") or \ (user_choice == "scissors" and cmptr_choice == "paper"): print("You won!") else: print("The Computer won!") play_again = input("Would you like to play again? (y/n) ").lower() if play_again == "y": return play_game() else: print("Thank you for playing.")
if __name__ == "__main__": play_game() elif gamechoice=="GG": from random import randint cont = "Y" while cont != "N": answer = randint(1, 10) print("Welcome to the guessing game!") for x in range(9): userans = int(input("Enter a number between 1 and 10: ")) if userans < answer: print("Incorrect! Please guess higher.") elif userans > answer: print("Incorrect! Please guess lower.") else: break if userans == answer: print("CONGRATS! You got it right in ", x+1, "tries! ") else: print("You only had 9 attempts. The answer was ", answer) cont = input("Would you like to play again? (Y for yes, N for no)") cont = cont.upper() print("Thank you for playing!")
else: print("Invalid Game") continue elif choice == "W": workchoice=input("Would you like to Create a Seq or Calc Work and Power? Press S for Seq or WP for Work and Power") workchoice=workchoice.upper() if workchoice=="S": #Seq code that calls its module and functions to run import sequence_functions
#Ask the user what list they want to create; once chosen ask the user how long they want the list, and what the starting number should be def answer(): seq = input("Choose a sequence you want the program to list off. (S,F,T) ").upper() #If they choose S, start the square function if seq == 'S': length = int(input("How long is your list? ")) start = int(input("What is the starting number? ")) print(sequence_functions.square(start,length)) #If they choose F, start the fibonacci function elif seq == "F": length = int(input("How long is your list? ")) print(sequence_functions.fibonacci(length)) #If they choose T, start the triangle function elif seq == "T": length = int(input("How long is your list? ")) start = int(input("What is the starting number? ")) print(sequence_functions.triangle(start,length)) #If the user chooses a invalid number in their list, tell them it was invalid else: print("Your number is invalid!") #If the user created a list, ask the user if they want to create another list, if so reset the answer function redo = input("Would you like to run this program again? (Y or N) ").upper() if redo == "Y": answer()
answer() elif workchoice=="WP": #Work and Power code that calls its module and functions to run #Make a function for work that calculates the equation of work done def work(mass, height, acceleration): work_done = mass * height * acceleration return work_done #Make a function for power that calculates the equation of power in joules def power(work_done, time): power_output = work_done / time return power_output #Make another function for power that calculates the equation of power in HP def horse_power(power_output): horse_power_output = power_output / 746 return horse_power_output #Make a function that tells the user to enter the mass, height, and time of the problem def main(): mass = float(input("What is the mass? (in kg): ")) height = float(input("What is the height of the stairs? (in m): ")) time = float(input("How long did it take to get up the stairs? (in sec): ")) #If the user's calculation is a negative number, tellthe user their values should be greater than zero if mass <= 0 or height <= 0 or time <= 0: print("All values should be greater than zero.") return #Calculate the correct answers using the previous function and inputted numbers acceleration = 9.8 work_done = work(mass, height, acceleration) power_output = power(work_done, time) horse_power_output = horse_power(power_output) #Print the user the work and power in joules and HP print("Your work done was: {:.2f} Joules".format(work_done)) print("Your power was: {:.2f} Watts".format(power_output)) print("Your power in Horse Power was: {:.2f}".format(horse_power_output)) #Start the functions when running the program using a module if __name__ == '__main__': main()
else: print("Invalid Work Choice") continue cont0=input("Would you like to Work or Play again? Y or N") cont0=cont0.upper()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
