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
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
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.
(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
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/