Python - 运行for循环3次

时间:2020-12-01 23:54:31

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

我不知道要添加什么,所以循环在找到最大数字后再运行2次

2 个解决方案

#1


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.

这将为您提供一个名为数字的列表,其中包含3个数字。然后你可以用它们做你想做的事。话虽如此,你真的不需要for循环来做到这一点。正如克雷格伯格勒所说。

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

或者(虽然这不使用范围...):

number1 = 0
number2 = 0
number3 = 0

for i in (number1, number2, number3):
    i = int(input("Enter number: "))

#2


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.

写入的代码将要求三次三个数字,覆盖用户输入的第一和第二组数字。如果分配是从用户获得三个数字并告诉用户哪个是最大的,那么您不需要for循环。三个输入语句将起到作用。

#1


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.

这将为您提供一个名为数字的列表,其中包含3个数字。然后你可以用它们做你想做的事。话虽如此,你真的不需要for循环来做到这一点。正如克雷格伯格勒所说。

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

或者(虽然这不使用范围...):

number1 = 0
number2 = 0
number3 = 0

for i in (number1, number2, number3):
    i = int(input("Enter number: "))

#2


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.

写入的代码将要求三次三个数字,覆盖用户输入的第一和第二组数字。如果分配是从用户获得三个数字并告诉用户哪个是最大的,那么您不需要for循环。三个输入语句将起到作用。