-5
for (length, freq) in word_list:
    print(freq_temp.format(length, freq))
print("\n Len  Freq Graph")
for (length, freq) in word_list:
    graph_template = "{:>4}{:>5}% {}"
    number_symbol = "=" * percentage_frequency(freq, new_list)
    print(graph_template.format(length, percentage_frequency(freq, new_list),number_symbol))

How would you convert these for loops into while loops?

Mark
  • 5,089
  • 2
  • 20
  • 31
Dan
  • 5
  • 3
  • 2
    Why do you want to convert it into a `while` loop? What's the issue you're running into using `for` loops? – Mark Oct 21 '18 at 08:25
  • 1
    What is a `while` loop is comparison to a `for` loop? – Matthieu Brucher Oct 21 '18 at 08:25
  • im just curious as to how you would do it. I have done other conversions but this one stumps me. – Dan Oct 21 '18 at 08:27
  • Possible duplicate of [Converting for loops to while loops in python](https://stackoverflow.com/questions/18900624/converting-for-loops-to-while-loops-in-python) – DarkSuniuM Oct 21 '18 at 08:31

1 Answers1

0

You are missing the point of for and while loops, this question does a good job of explaining it.

Basically, a for loops iterate through the list and you can operate on the items as it does so.

In contrast, while loop is used to run until a condition is met, such as a flag being triggered.

The for loop:

mylist = ["this", "is", "a", "for", "loop"]
for element in mylist:
    print(element)

Returns:

this
is
a
while
loop

Where as this while loop:

count = 0
while count != 10:
    print(count)
    count += 1
print("count has reached 10")

Returns:

0
1
2
3
4
5
6
7
8
9
count has reached 10

In summary, a for loop is used to iterate through an array or generator object, where as a while loop is used to run until a condition is met.

13smith_oliver
  • 414
  • 6
  • 13