8

So I have this assignment and I have a question about a part I don't know how to do, can you guys help me?

def main():

 # Please see the comments 

    largest = 0

    for index in range(3):  # Enter the value(s) in the parenthesis to run the loop 3 times
        number1 = int(input("Please enter the first number: "))

        number2 = int(input("Please enter the second number: "))

        number3 = int(input("Please enter the third number: "))



    # insert call to function find_largest after this comment.
    # find_largest will take in three parameters and will return the largest of the 3 numbers

    result = find_largest(number1, number2, number3)

    # insert the statement to print the three numbers entered and the largest number after this comment.
    print("The numbers you entered were, \n", [number1, number2, number3]) 
    print ("The largest of the numbers you entered is", result)


def find_largest(a, b, c):

    # insert parameters in the parenthesis
    # Write the code for this function here.
    # find_largest will take in three parameters and will return the largest of the 3 numbers
    # These three numbers are passed in as parameters from the main function
    # Hint: if and elif - no loop needed here

    if (a > b) and (a > c):
       largest = a
    elif (b > a) and (b > c):
       largest = b
    else:
       largest = c

    return largest


main()    # this is the call to main() that will make the program run

So, my question is the part:

for index in range(3):  # Enter the value(s) in the parenthesis to run the loop 3 times

I don't know what to add so the loop run 2 more times after it has found the largest number

Vink
  • 89
  • 1
  • 1
  • 2
  • Is the loop running three times in order to get the three numbers that will be used for the statistics? Or do you actually want to *get a new group of three numbers, three times*, and do `find_largest` a total of three times (once with each group)? Or just what? – Karl Knechtel Jul 30 '22 at 00:37

2 Answers2

15

The loop you have makes the first two iterations of the loop pointless, as each time you loop, you are reassigning new numbers to the three number variables. As a result, only the numbers entered in the last iteration of the loop are ever used for anything. I think this would make more sense:

numbers = []

for i in range(3):
    input = int(input("Enter number: "))
    numbers.append(input)

This will give you a list called numbers with 3 numbers in it entered by the user. Then you can do what you want with them. Having said that, you really don't need a for loop to do this. As Craig Burgler mentioned.

Alternatively(though this doesn't use range...):

number1 = 0
number2 = 0
number3 = 0

for i in (number1, number2, number3):
    i = int(input("Enter number: "))
Totem
  • 7,189
  • 5
  • 39
  • 66
  • Yeah, I think that makes more sense. Thanks! – Vink May 02 '15 at 18:11
  • 1
    He can also use max(numbers) instead of using the algorithm if he is using a list. However, nice to see people making it their own instead of the easy route. – fingaz May 02 '15 at 18:12
0

The code as written will ask for three numbers three times, overwriting the first and second set of numbers that the user enters. If the assignment is to get three numbers from the user and tell the user which is largest, then you do not need the for loop. The three input statements will do the trick.

Craig Burgler
  • 1,749
  • 10
  • 19
  • Yeah, I noticed that, my teacher specifically wrote this but never said if we could overwrite it, --> 1. The loop needs to run 3 times. Enter a value in the parenthesis after the keyword range to ensure that the loop runs 3 times. – Vink May 02 '15 at 18:01
  • So you can have one input statement within the `for` loop and maybe store the user entries in a list: `numbers.append(int(input(...)))` – Craig Burgler May 02 '15 at 18:04
  • I mean that part of the code was written by the teacher not by me... I'm just filling the gaps and that's the only part that's had me guessing – Vink May 02 '15 at 18:05