Please forgive me… new to python and first question on stack.
I’m trying to accomplish two things with while loop:
- increment round number (i’m doing this successfully)
- have the loop iterate over my variable “fruits” to print the first fruit on round one and second fruit on round two
variables:
round_number = 0 fruit = [apple, orange, banana]
python code:
while round_number < 5:
    round_number += 1
    print("Round Number: ", round_number) 
 for item in fruit:
      print(item)
the above code is what I currently have. When I run the code it prints all items in my fruit variable at once.
My desired output is for the print output to look like this:
Round Number: 1 apples
Round Number: 2 oranges
ect…
Advertisement
Answer
If you must use the while-loop then you can write your code as this:
fruit = ['apple', 'orange', 'banana']
round_number = 0
while round_number < len(fruit):
    round_number += 1
    print("Round Number: ", round_number, fruit[round_number-1]) 
and for the for-loop then your code will be:
fruit = ['apple', 'orange', 'banana']
round_number = 0
for item in fruit:
    print("Round Number: ", fruit.index(item)+1, item)
hope this help