Python - 运行 for 循环 3 次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30005590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - Run for loop 3 times
提问by Vink
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 次
回答by Craig Burgler
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
循环。三个输入语句可以解决问题。
回答by Totem
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.
这将为您提供一个名为 numbers 的列表,其中包含用户输入的 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: "))