python for loop 2程序具有不同的输出

时间:2018-08-01 17:39:29

标签: python for-loop

我在用for循环练习,我在这里很菜:p

情况1-

Fruits = ['mango','banana','orange','grapes','apple']
for fruit in Fruits:
    print("Current fruit:",fruit)
print("Good Bye")

此代码的输出为

Current fruit: mango
Current fruit: banana
Current fruit: orange
Current fruit: grapes
Current fruit: apple
Good Bye

情况2-

students = ['Avinash','Arush','Akshay','Raghav','Aditya']
for student in students:
    print("Current student are:",students)
print("meet you next time")

输出

Current student are: ['Avinash', 'Arush', 'Akshay', 'Raghav', 'Aditya']
Current student are: ['Avinash', 'Arush', 'Akshay', 'Raghav', 'Aditya']
Current student are: ['Avinash', 'Arush', 'Akshay', 'Raghav', 'Aditya']
Current student are: ['Avinash', 'Arush', 'Akshay', 'Raghav', 'Aditya']
Current student are: ['Avinash', 'Arush', 'Akshay', 'Raghav', 'Aditya']
meet you next time

1 个答案:

答案 0 :(得分:3)

print("Current student are:",students)

在这里,您要打印整个list个学生,而不是当前变量student。应该是

print("Current student are:",student)

这确实有助于拥有不同的变量。