Posts: 2
Threads: 1
Joined: May 2019
May-27-2019, 08:23 AM
(This post was last modified: May-27-2019, 08:52 AM by buran.)
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
Posts: 2,168
Threads: 35
Joined: Sep 2016
- 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.
- 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.
Posts: 1,950
Threads: 8
Joined: Jun 2018
May-27-2019, 11:31 AM
(This post was last modified: May-27-2019, 11:31 AM by perfringo.)
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.
Posts: 4
Threads: 1
Joined: May 2019
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.
Posts: 2
Threads: 1
Joined: May 2019
(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
Posts: 2,125
Threads: 11
Joined: May 2017
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/
|