Question: In python, rewrite the code and create a module. Have at least 2 separate files for functions the create your sequence and your main program.
In python, rewrite the code and create a module.
Have at least 2 separate files for functions the create your sequence and your main program.
#This program asks the user for a sequence, and gives them a list using the inputed data #Using a function, create a method that gives a proper list for the fibonacci numbers def fibonacci(num, current = 1, previous = 0): #If the inputted number is lesser than 0, don't print the list if num <= 0: return [] #If the number is above zero, calculate the correct equation to give the correct list else: data = [previous] number = fibonacci(num - 1,current + previous,current) data = data + number return data
#Do the same for the square numbers def square(current, length): if length<=0: return [] data = [current * current] number = square(current + 1,length - 1) data = data + number return data
#And the same for the triangle numbers def triangle(current, length): if length<=0: return [] data = [current * (current + 1)/2] number = triangle (current + 1,length - 1) data = data + number return data
#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(square(start,length)) #If they choose F, start the fibonacci function elif seq == "F": length = int(input("How long is your list? ")) print(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(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()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
