Home > Back-end >  How to create a list of data that depends on three inputs? [closed]
How to create a list of data that depends on three inputs? [closed]

Time:09-21

I am trying to create a Python program that depends on three variables. I am new to this so am not entirely sure how to proceed. The goal is to create a list that shows time passed. I am supposed to be inputting this data:

  1. The number of minutes per session
  2. The initial time
  3. The number of sessions

The program is supposed to look like this:

-Input the length of the session: 20

-Enter the initial time: 800

-Enter the number of sessions to display: 5

Session #0, Time = 800

Session #1, Time = 820

Session #2, Time = 840

Session #3, Time = 860

Session #4, Time = 880

I have almost everything figured out with how to input the data itself, and how to create an equation for the final time. I just don't know how to print a list that has a number of outputs that depend on the variable entered.

perSession = (int(input("Enter the time per session:/n"))
initalTime = (int(input("Enter the inital time:/n"))
sessionNumber = (int(input("Enter the number of sessions:/n"))
totalTime = (InitialTime   perSession * sessionNumber)
               
print("Session #", SessionNumber, "Time =", totalTime)

Is what I have so far.

CodePudding user response:

You can do this in different ways, one could be this:

perSession = (int(input("Enter the time per session: ")))
initalTime = (int(input("Enter the inital time: ")))
sessionNumber = (int(input("Enter the number of sessions: ")))
list = []

for i in range (sessionNumber) :
   list.append(initalTime   perSession * i)

print(list)

You should reaed Python docummentation on how to work with lists.

CodePudding user response:

This should do what you want. The bit you are probably most interested in is the session_formatter function. Also note I'm using a generator function gen_session_times because enumerate can evaluate in a lazy manner. Basically to solve your issue you just need to iterate over the "list" you mentioned with a for loop and execute a print statement that formats the current value.

def get_user_input():
    """
    Get user input
    """
    session_length = input("Number of minutes per session?")
    initial_time = input("Initial time in minutes?")
    number_of_sessions = input("Number of sessions?")
    return (session_length, initial_time, number_of_sessions)


def gen_session_times(session_length, initial_time, number_of_sessions):
    """
    Generate session times
    """
    for _ in range(number_of_sessions):
        yield initial_time
        initial_time  = session_length


def session_formatter(session_num, session_time):
    """
    Format session times
    """
    return f"Session #{session_num}, Time = {session_time}"


def main():

    # get user input
    session_length, initial_time, number_of_sessions = get_user_input()

    # convert user input to integers
    session_length = int(session_length)
    initial_time = int(initial_time)
    number_of_sessions = int(number_of_sessions)

    # calculate total time
    total_time = (session_length * number_of_sessions)   initial_time

    # display total time
    print("Total time:", total_time, "minutes")

    # display session times
    for session_num, session_name in enumerate(
        gen_session_times(session_length, initial_time, number_of_sessions)
    ):
        print(session_formatter(session_num, session_name))


if __name__ == "__main__":
    main() 

CodePudding user response:

If you just need a list:

per_session = (int(input("Input the length of the session: ")))
initial_time = (int(input("Enter the initial time: ")))
session_number = (int(input("Enter the number of sessions to display: ")))

list_of_outputs = [initial_time, ]
for s in range(1, session_number):
    list_of_outputs.append(initial_time   per_session)
    initial_time  = per_session

print("List: {0}".format(list_of_outputs))

Output:

Input the length of the session: 20
Enter the initial time: 800
Enter the number of sessions to display: 5
List: [800, 820, 840, 860, 880]
  • Related