I need to print certain value if the value of the user input repeats 2 consecutive times (Python)

Viewed 73

Hi I'm taking a beginners class in python and can't seem to understand 2 things of the project I'm working on.

  1. How can I make a sum of the values introduced in a loop?
  2. How can I use the if function to print out values and end the program if the user introduces the same value twice in a row

This is what I have now:

def main():
    # Length of the period (in months)
    a = 0
    number_of_months = input("Enter the number of months: ")
    #Variable for sum
    total_sum = 0
    #Convert to int
    b = (int(number_of_months))
    #The program reads the points of each month
    while a < b:
        #Assign a numerical value to each month
        for n in range (b):
            a = a + 1 #Asign a number per month
            points = int(input(f"Enter the number of credits in month {a}:"))
            #Sum of the points
            for i in range(points):
                total_sum = points + total_sum
            print(total_sum)
            #Average


            #Determine if the user qualifies as full time student

            #Determine if the user is a full time student

            #Calculate the avarage
    if __name__ == "__main__":
        main()

This is the assignment I'm working on:

At first the program waits for the user to input the length of the period (in months) during which the credit point gain is followed:

Enter the number of months: After the length of the time period has been entered the program starts reading the credit point gains for each month:

Enter the number of credits in month X: Where X is the ordinal number of the month whose credit point gain should be entered. Note, that this is not a month number, but a running ordinal which always starts from 1.

If the user has not gained any credits during 2 consecutive months, the program immediately prints:

You did have too many study breaks!

and quits without any other printouts.

Otherwise, after the user has entered the credit point gains for all the months during the period considered, the program calculates and reports if the user is considered a full time student or not.

If the user is a full time student (average monthly credit gain is 5 or more), the program will print:

You are a full time student and your monthly credit point average is Y.Y.

where Y.Y is the average of the monthly credit point gains printed using one decimal accuracy.

Otherwise the program prints:

Your monthly credit point average Y.Y does not classify you as a full time student.

Again Y.Y means the average credit point gain using one decimal.

1 Answers

You're making this way harder than it needs to be. Just keep each answer in a list. That way, you can compare the newly entered value with the last item in the list. After you're done, you can sum the list and divide by the length.


nmo = int(input('Enter number of months: '))

points = []
for mo in range(nmo):
    pts = int(input(f"Enter credits in month {mo+1}: "))
    if points and pts == 0 and points[-1] == 0:
        print( "You had too many study breaks." )
        sys.exit(0)
    points.append( pts )

average = sum(points) / len(points)
if average >= 5:
    print( "Full time." )
else:
    print( f"{average} is Not full time." )
Related