Skip to content Skip to sidebar Skip to footer

Reject Or Loop Over User Input If Two Conditions Not Met

I am a real beginner with Python, although I am loving every minute of it so far. I am making a little program that takes user input and then does stuff with it. My issue is that t

Solution 1:

input_list = []
input_number = 1
while True:

    input_list.append(raw_input('Enter percentage {} (in decimal form):'.format(input_number))

    if float(input_list[-1]) > 1:     # Last input is larger than one, remove last input and print reason
        input_list.remove(input_list[-1])
        print('The input is larger than one.')
        continue

    total = sum([float(s) for s in input_list])
    if total > 1:    # Total larger than one, remove last input and print reason
        input_list.remove(input_list[-1])
        print('The sum of the percentages is larger than one.')
        continue

    if total == 1:    # if the sum equals one: exit the loop
        break

    input_number += 1

Post a Comment for "Reject Or Loop Over User Input If Two Conditions Not Met"