Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop
#1
to the question:
Write a while loop to display the values of the Rating of an album playlist stored in the list PlayListRatings. If the score is less than 6, exit the loop. The list PlayListRatings is given by: PlayListRatings = [10, 9.5, 10, 8, 7.5, 5, 10, 10]

the answer was
PlayListRatings = [10, 9.5, 10, 8, 7.5, 5, 10, 10]
i = 1
Rating = PlayListRatings[0]
while(Rating >= 6):
    print(Rating)
    Rating = PlayListRatings[i]
    i = i + 1
i did not understand why
expecially why - Rating = PlayListRatings[0]

why not write something like this:
PlayListRatings = [10, 9.5, 10, 8, 7.5, 5, 10, 10]
i = 0
Rating = PlayListRatings[i]
while(Rating >= 6):
    print(Rating)
    i = i + 1
i probably did not understand something about the coding
Reply
#2

  1. The first code sets rating to the first item of the list before the while loop, it then updates rating by each items index in the list.

  2. The second code would be an infinite loop because it sets rating to the first item of the list before the while loop and then never changes.
Reply
#3
As a snob I point to PEP8.

From Function and Variable Names:

Quote:Function names should be lowercase, with words separated by underscores as necessary to improve readability.

Variable names follow the same convention as function names.

mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.

Also alternative while loop, just for fun:

In [1]: playlist_ratings = [10, 9.5, 10, 8, 7.5, 5, 10, 10]                    

In [2]: while playlist_ratings: 
   ...:     if playlist_ratings[0] < 6: 
   ...:         break 
   ...:     else: 
   ...:         print(playlist_ratings.pop(0)) 
   ...:                                                                                  
10
9.5
10
8
7.5
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
The while loop is similar to a for loop in the exception that it occurs until something does not equal.Then the while loop will stop.A normal for loop will only stop when a condition is met.
Reply
#5
(May-27-2019, 11:31 AM)perfringo Wrote: As a snob I point to PEP8. From Function and Variable Names:
Quote: Function names should be lowercase, with words separated by underscores as necessary to improve readability. Variable names follow the same convention as function names. mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.
Also alternative while loop, just for fun:
 In [1]: playlist_ratings = [10, 9.5, 10, 8, 7.5, 5, 10, 10] In [2]: while playlist_ratings: ...: if playlist_ratings[0] < 6: ...: break ...: else: ...: print(playlist_ratings.pop(0)) ...: 10 9.5 10 8 7.5 

yes!
that is more understandable
the other methods i still could not figure their logic
Reply
#6
You can use itertools.takewhile.
First you need a function, which takes one argument and does the check.

# nothing special
def condition(value):
    return value >= 6
Then you need to import the functions from itertools.

from itertools import takewhile
from itertools import dropwhile


playlist_ratings = [10, 9.5, 10, 8, 7.5, 5, 10, 10]

# Return successive entries from an iterable as long as the 
# predicate evaluates to true for each entry.
result_1 = list(takewhile(condition, playlist_ratings))

# Drop items from the iterable while predicate(item) is true.
# Afterwards, return every element until the iterable is exhausted.
result_2 = list(dropwhile(condition, playlist_ratings))
The function condition could also written as lambda expression: lambda x: x >= 6
result_3 = list(takewhile(lambda x: x >= 6, playlist_ratings))
Another function which takes a conditional function, is filter and map.
Filter out all values where the condition is False:
result_4 = list(filter(condition, playlist_ratings))
Here you can find a good article: https://realpython.com/python-itertools/
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020