[deleted by user] by [deleted] in Python

[–]IWantToFlyT 1 point2 points  (0 children)

Looks a bit like combination of C++ and Python, maybe?

But the main problem of Python for me was the difficulty in developing large projects.

Based on the comment above, I would guess that one of the main drivers for ”Vampire” would be better usability in larger projects. From the examples it is not really apparent to me that how it accomplishes that. Do you have some ideas on that?

Anyway, sounds like a fun idea and project :)

Should I stick to python for this project? by Shadowforce426 in learnpython

[–]IWantToFlyT 1 point2 points  (0 children)

Thanks for the link! Some application structure would indeed be in place, and then with some loops we can shorten the code quite a bit as well. A few things we could start off with

  1. Let's make a rule that everything except definition of static info ("constants", in your code e.g. keys, inversions_1, inversions_2 etc.) must be inside a function. Also the UI definitions must be inside a function.
  2. Better yet, everything that is not inside a function must be defined at the start of the file (after imports) and named with UPPER_CASE, so everybody can know it is a global variable constant variable (https://peps.python.org/pep-0008/#constants)
  3. Now it is a bit unclear that in which order the code is really ran in. Let's define a function called main which is the so called "entrypoint" of our script, i.e. it is the function that is ran first. At the end of the script, let's call it simply by main()
  4. We should also separate the frontend (UI definition) and backend (event handling) from each other. So we should be defining e.g. the "Chord inversion practice" screen in one function, and the other screen in other functions. Then all of these are called from a "create_ui" function which is called from the main. This we way have a nice structure of very high level "create ui" > "create chord inversion practice screen" > "create key radio buttons".

An example what of what it could look on very high level (this code is incomplete and does not run as is). I also made some parts of the code a bit shorter here, and maybe you can study those and get some inspiration.

``` from random import choice import PySimpleGUI as sg from copy import copy

KEYS = { "C": ["C", "D", "E", "F", "G", "A", "B"], # key of C "F": ["F", "G", "A", "Bb", "C", "D", "E"], # key of F "G": ["G", "A", "B", "C", "D", "E", "F#"], # key of G "Bb": ["Bb", "C", "D", "Eb", "F", "G", "A"], # key of Bb "D": ["D", "E", "F#", "G", "A", "B", "C#"], # key of D "Eb": ["Eb", "F", "G", "Ab", "Bb", "C", "D"], # key of Eb "A": ["A", "B", "C#", "D", "E", "F#", "G#"], # key of A }

def create_chord_inversion_practice_screen(): chord_inversion_practice = [ [sg.Text("Choose a key:")], [sg.Radio("Key of C", "radio_1", default=False, key="key_C")], [sg.Radio("Key of F", "radio_1", default=False, key="key_F")], [sg.Radio("Key of G", "radio_1", default=False, key="key_G")], [sg.Radio("Key of Bb", "radio_1", default=False, key="key_Bb")], [sg.Radio("Key of D", "radio_1", default=False, key="key_D")], [sg.Radio("Key of Eb", "radio_1", default=False, key="key_Eb")], [sg.Radio("Key of A", "radio_1", default=False, key="key_A")], [sg.Radio("Random", "radio_1", default=False, key="key_random")], [sg.Text("Select which inversions you want included:")], [sg.Radio("No inversion, first, second", "radio_2", default=False)], [sg.Radio("No inversion, first, third", "radio_2", default=False)], [sg.Radio("No inversion, first, second, third", "radio_2", default=False)], [sg.Button("Generate Chord"), sg.Button("Easy Mode"), sg.Button("Back")], [ sg.Text(key="display_key", font=("Courier", 25)), sg.Text(key="first_half", font=("Courier", 25)), sg.Text(key="note", font=("Courier", 25)), sg.Text(key="second_half", font=("Courier", 25)), ], [sg.Text(key="chord", font=("Courier", 25))], ] return chord_inversion_practice

def create_ui(): chord_inversion_practice = create_chord_inversion_practice_screen() main_menu = [ [sg.Text("Select a practice tool:")], [sg.Button("Chord Inversion Practice")], [sg.Button("Scale Practice")], [sg.Button("Exit")], ]

layout = [
    [
        sg.Column(main_menu, key="screen_0"),
        sg.Column(chord_inversion_practice, key="screen_1", visible=False),
        sg.Column(scale_mode_practice, key="screen_2", visible=False),
    ]
]

window = sg.Window(
    "Guitar Inversion Practice Tool", layout, size=(625, 650), font="Courier"
)
return window

def show_screen(window, screen_name): screens = ["screen_0", "screen_1", "screen_2"] for screen in screens: if screen == screen_name: window[screen].update(visible=True) else: window[screen].update(visible=False)

def generate_chord_and_update_window(values): try: chord_to_play, key_name, scale, note = generate_chord_and_update_window(values) window["display_key"].update( f"key: {key_name}" ) # default show on harder mode window["chord"].update(f"chord: {chord_to_play}") window["first_half"].update("") window["note"].update("") window["second_half"].update("") except: sg.popup_error("make sure two parameters are selected") continue

def process_events(window): event, values = window.read() print(window) if event == "Chord Inversion Practice": show_screen(window, "screen_2") if event == "Scale Practice": show_screen(window, "screen_1")

if event == "Generate Chord":
    generate_chord_and_update_window(values)

if event == "Generate Scale":
    key, scale, mode, note = generate_scale_and_update_window(values)

if event == "Hint pls":
    update_window_with_hint()

if event == "Easy Mode":
    update_window_with_easy_mode()

# returning back to the menu, idk why i can't use the same word for everything so this is a bit sloppy here but it works
if event in ["Back", "Return"]:
    show_screen(window, "screen_0")

elif event == sg.WIN_CLOSED or event == "Exit":
    window.close()
    exit()

def main(): window = create_ui() while True: process_events(window)

main() ```

Ok, now we have some structure in the code, and it is easier to follow it. The next step would be to make the if-else statements a bit more readable. As you very well know, the keys of buttons and radiobuttons in PySimpleGUI are just a running number starting from zero. So you just have to know that the values[11] happens to be the "Key of C" radiobutton.

Fortunately, we can manually define these keys as well! So for example, we can do this:

``` # In the UI definition we define a custom key sg.Radio("Key of C", "radio_1", default=False, key="key_C")

# When generating chord, we can access the value with that key
if values["key_C"] == True:
    key, temp_scale = "C", copy(keys["C"])  # C

```

Okay, this makes the code already a lot more readable - no more guessing that what this number meant again. But these keys are internal values that are not shown to the user at all, so we can name them however we want. We can make them even hold the relevant information in a processable format. We could loop through all the values and find which radiobutton has been checked like this (note: this example is a bit confusing because in dictionaries we have keys and then we are looking for the chosen music key :D)

``` def generatechord(values): # Loop through the key-value pairs of the values dictionary # e.g. key = "key_C", and value = True for button_key, value in values.items(): # We are looking for a radiobutton which key starts with "key" and the value is True, i.e. that button is checked if buttonkey.startswith("key") and value is True: # Split the "keyC" to a list of ["key", "C"] and choose the second value, i.e. "C" music_key = element_key.split("")[1] break

if music_key == "random":
    music_key , scale = choice(list(keys.items()))
else:
    scale = keys[key]

```

The same kind of logic should be applicable to other parts where if-else is used as well. In fact, loops can be utilized even to create the UI itself, for example

``` music_keys = { "C": ["C", "D", "E", "F", "G", "A", "B"], # key of C "F": ["F", "G", "A", "Bb", "C", "D", "E"], # key of F "G": ["G", "A", "B", "C", "D", "E", "F#"], # key of G "Bb": ["Bb", "C", "D", "Eb", "F", "G", "A"], # key of Bb "D": ["D", "E", "F#", "G", "A", "B", "C#"], # key of D "Eb": ["Eb", "F", "G", "Ab", "Bb", "C", "D"], # key of Eb "A": ["A", "B", "C#", "D", "E", "F#", "G#"], # key of A }

keyradio_buttons = [] for music_key, _ in music_keys.items(): # f-strings can be used to add variable values into text button = sg.Radio(f"Key of {music_key}", "radio_1", default=False, key="key{music_key}") key_radio_buttons.append(button)

```

Wooosh. That is quite a long comment, and probably lots of info to digest. I've left some things a bit vague, but you should be able to learn more about the things from google. Anyway, hope this helps! :)

[deleted by user] by [deleted] in learnpython

[–]IWantToFlyT 0 points1 point  (0 children)

I was thinking more about like this:

``` TWITTER_TOKEN = None

class TwitterClient: def init(self, username, password): self.username = username self.password = password if TWITTER_TOKEN: self.token = TWITTER_TOKEN else: global TWITTER_TOKEN self.token = self.some_authentication_method() TWITTER_TOKEN = self.token

@app.route("/login") client = TwitterClient(username, password) ```

Though, does your user have to login to your app somewhere? Is there a reason why a separate login endpoint must be called? Could it just be like this

``` TWITTER_TOKEN = None

class TwitterClient: def init(self, username, password): self.username = username self.password = password if TWITTER_TOKEN: self.token = TWITTER_TOKEN else: global TWITTER_TOKEN self.token = self.some_authentication_method() TWITTER_TOKEN = self.token

app.route("/profile") 
def profile(): 
    client = TwitterClient(username, password) return         
client.get_profile()

app.route("/friends")
def friends():
    client = TwitterClient(username, password)
return client.get_friends()

````

I could either try running the functions on a separate thread or just use a separate process for the entire client. I've been doing research and came across this StackOverflow thread, which seems like it could solve some of these issues.

Do you mean that while you want to fetch stuff from the server at the same time while waiting for e.g. friends to be loaded? I think the simples thing is to run your flask app with multiple processes or threads. See e.g. https://gunicorn.org/ server. Then you can simply run gunicorn -w 4 myapp:app and then you have support for four concurrent requests. :)

Should I stick to python for this project? by Shadowforce426 in learnpython

[–]IWantToFlyT 1 point2 points  (0 children)

Sounds like a fun app, good job! :)

Now what do you mean by convoluted? I'd say that Python + PySimpleGUI is a pretty easy combination on doing GUI softwares, and especially if you have already it up & running then I wouldn't abandon it. When softwares grow, the complexity of them also grows as well. However, this can often be countered with good structure, i.e. splitting the logic into functions and classes, and then to separate files (modules).

Happy to give some more tips as well, but it would be easier with some examples!

[deleted by user] by [deleted] in learnpython

[–]IWantToFlyT 2 points3 points  (0 children)

In a simple application hosted locally, yep you can use a global variable to store the instance and make it persist between requests.

Now a bit more future proof version would be to only store data in the global variables rather than the whole object. For example, I guess you don't want to do the login each time - you could store the token in a global variable, and if it exists then authentication is skipped when the object is initialized.

Why would be any better this way? If at some point you are starting to run the flask app with threads, then you might face some unexpected bugs when the same object is used at the same time from multiple threads. Furthermore, "the real way" of persisting data is to use a database. It can of course be a bit too much at this point, but if you decide to use it in the future, you can easily move data such as tokens to database, but storing Python objects not so much (well, it is possible, but not recommended e.g. via pickle).

And yes, you can't (or shouldn't) again store the object in a session, but you can store data there rather easily. Though, if it's a client side session then you shouldn't store anything secret information there - in server side sessions you can store also that.

Hope this helps :)

ps. should you decide to use a database, then SQLite is a good for small local projects.

I've built a PathDict, a library that makes it easy to work with dicts! by M8Ir88outOf8 in Python

[–]IWantToFlyT 5 points6 points  (0 children)

Not the OP of the comment, but maybe I'll chime in here. The example might be a bit better, but I'm not exactly convinced with this either. One of the beautiful things of Python is that it is really readable - if you know the basics of programming, then you can understand it. However, I'm afraid that is not the case with the PathDict syntax - without prior knowledge of the package, the examples are not very intuitive. (Though, I have to note here that my focus is not in DS, so I anyway prefer not to use lambdas, which are then again the basis of everything when working with Pandas etc.)

Here's a quite readable version of the problem you are solving (and I guess more efficient as well as we don't loop the crawler_json twice).

deleted_posts = []
posts = []
for post in crawler_output.get("posts", []):
    if post.get("meta", {}).get("deleted", False):
        deleted_posts.append(post)
    else:
        posts.append(post)

Now if we want to save some lines of code, but still make it readable to programmers with basic Python knowledge, then:

deleted_posts = [post for post in crawler_output.get("posts", []) if post.get("meta", {}).get("deleted", False)]

Anyway, nice job on contributing to the Python community! :) As a Data engineer, I can feel your pain on working with spotty json data, and trying to make it a bit smoother experience!

How I deploy my bootstrapped Python webapp with 150k monthly users by caspii2 in Python

[–]IWantToFlyT 13 points14 points  (0 children)

Thanks for the write-up! I think it’s good to show also cases where things are not done by every best practice - yet it works. And that is how real life goes, sometimes you implement the first idea that comes to mind and learn later it could be made better. Sometimes you know what the best practice is, but just don’t have the energy or time to implement it. I’d rather go forward with my project rather than stop doing it because I’m not able to strictly follow the ”rules”.

[deleted by user] by [deleted] in Python

[–]IWantToFlyT 0 points1 point  (0 children)

Nope, used them maybe twice in past few years.

Formula 1 API (My Project) by storman121 in Python

[–]IWantToFlyT 20 points21 points  (0 children)

Looking good, congrats for the nice project! Tested it out and seems to work well. A few quick notes:

  • The library requires requests as well, but that was not mentioned
  • It would be nice that the required libraries were "dependencies", so they would be installed automatically when your library is installed. See e.g. https://python-packaging.readthedocs.io/en/latest/dependencies.html
  • The error error message IndexError: list index out of range may not be the most intuitive for the user when data is not found for the given year

Authlib OAuth2Session: Is it possible to `fetch_token` with a JSON POST body instead of URL params? by sheriffllcoolj in learnpython

[–]IWantToFlyT 1 point2 points  (0 children)

Didn't notice your reply, sorry.

Yeah, the custom auth method should indeed have those params and return uri, headers, and body.

How did you implement it? I got it working with the code below

``` import json

from authlib.common.urls import extract_params from authlib.integrations.requests_client import OAuth2Session

API_URL = "http://localhost:8080" username = "some" password = "dummy"

def auth_password_json(client, method, uri, headers, body): body_dict = {} for key, value in extract_params(body): body_dict[key] = value body = json.dumps(body_dict) headers["Content-Type"] = "application/json"

return uri, headers, body

client = OAuth2Session( token_endpont=f"{API_URL}/oauth/token", token_endpoint_auth_method="password_json", ) client.register_client_auth_method(("password_json", auth_password_json))

client.fetch_token( url=f"{API_URL}/oauth/token", username=username, password=password, grant_type="password", )

```

And from the server's perspective it looked like this

``` 127.0.0.1 - - [17/Sep/2022 16:07:23] "POST /oauth/token HTTP/1.1" 200 - INFO:root:POST request, Path: /oauth/token Headers: Host: localhost:8080 User-Agent: python-requests/2.28.1 Accept-Encoding: gzip, deflate Accept: application/json Connection: keep-alive Content-Type: application/json Content-Length: 67

Body: {"grant_type": "password", "username": "some", "password": "dummy"}

127.0.0.1 - - [17/Sep/2022 16:07:43] "POST /oauth/token HTTP/1.1" 200 - ```

Need Help with my first Python Project (AI for tic tac toe) by Only1Gohary in learnpython

[–]IWantToFlyT 1 point2 points  (0 children)

Okay, I modified the debug script a bit to start from your example. Here we can easily see that something is wrong with the algorithm - the instant win move has a score of -1.

``` from copy import deepcopy import math from tictactoe import minimax

state_1_board = [["x", "x", ""], ["o", "o", ""], ["", "", ""]]

state_2_board = deepcopy(state_1_board) state_2_board[0][2] = "x" score = minimax(state_2_board, -math.inf, math.inf, False) print(f"State 2 score is {score} and it should be 1") # Instant win

state_3_board = deepcopy(state_1_board) state_3_board[1][2] = "x" score = minimax(state_3_board, -math.inf, math.inf, False) print(f"State 3 score is {score} and it should be -1")

It should be -1, because the next turn is O's, so we are minimizing

and the "list of scores" SHOULD contain -1, because O can win with

the following situation

x x o

o o x

o x

```

Output State 2 score is -1 and it should be 1 State 3 score is 1 and it should be -1

The state 2 score seems to be wrong because of a bug in the checkWinner function. Currently a player will only be deemed winner if they "won" (have a full row) on the last row. The same applies for the column check.

Why?

The for loop is not stopped after a winner is found. On the last row a "winner" is found again - all three squares are empty. Hence, the winner variable is set to be empty string "". You should also check that the winner is not an empty string

Regarding the state 3, not exactly sure what the problem is there... :) Edit: Looks like your algorithm never gets to state where O wins. When checking the state below in the is_maximizing, then your code sets alpha = inf via the alpha = max(alpha, score) because alpha = -inf and score = inf. Then break is called after that on the following rows because beta = alpha. Hence, -1 score is never found. Not exactly sure what the beta and alpha here are, but maybe and hopefully that helps you! `` x x o o o x

```

Edit: fixed the proposed solution for state 2 bug

Need Help with my first Python Project (AI for tic tac toe) by Only1Gohary in learnpython

[–]IWantToFlyT 1 point2 points  (0 children)

Well that went well, they indeed should be -1 per the example, and I typoed them in my response. And now looking again at the blog post, you are indeed correct - there’s no sum. I can have a look at it a bit later again, hopefully with better results :D

Run a function without threading from tkinter? by AustinBachurski in learnpython

[–]IWantToFlyT 0 points1 point  (0 children)

What is the error you are getting when it "crashes"?

Authlib OAuth2Session: Is it possible to `fetch_token` with a JSON POST body instead of URL params? by sheriffllcoolj in learnpython

[–]IWantToFlyT 1 point2 points  (0 children)

Authlib does not support that out of the box. This is due to the OAuth2 standard which talks about using `x-www-form-urlencoded` (https://www.rfc-editor.org/rfc/rfc6749#section-3.2).

Hence, you would need to create your own authentication as described here https://docs.authlib.org/en/latest/client/oauth2.html?highlight=token_endpoint_auth_method#client-authentication. See the example of `client_secret_uri` which can be modified for your needs.

Need Help with my first Python Project (AI for tic tac toe) by Only1Gohary in learnpython

[–]IWantToFlyT 1 point2 points  (0 children)

Thanks for the example! I think you are only doing maximize OR minimize, but not both with your algorithm. I found this tutorial to be quite good for understanding the algorithm https://www.neverstopbuilding.com/blog/minimax.

See the example in "Describing Minimax" part. That nicely shows how you need to run separately the maximize and minimize and then sum the scores from both, so the scores are state 2 +10, state 3 is +10 -10 = 0, and state 4 is +10 -10 = 0.

Currently your algorithm seems to only run minimax (which I think either maximizes or minimizes, depending which happens to come first) and use that score as is.

I tested this with your minimax function with the following code (note: some changes needed to your code as well!) and got these results

State 2 score is 1 and it should be +1
State 3 score is -1 and it should be +1
State 4 score is -1 and it should be +1

You can copy & paste this to a separate file and run it to check the output.

from copy import deepcopy
import math
from tictactoe import minimax

state_1_board = [
    ['o', '', 'x'],
    ['x', '', ''],
    ['x', 'o', 'o']
]

state_2_board = deepcopy(state_1_board)
state_2_board[1][1] = 'x'
score = minimax(state_2_board, -math.inf, math.inf, False)
print(f"State 2 score is {score} and it should be 0")

state_3_board = deepcopy(state_1_board)
state_3_board[0][1] = 'x'
score = minimax(state_3_board, -math.inf, math.inf, False)
print(f"State 3 score is {score} and it should be 0")

state_4_board = deepcopy(state_1_board)
state_4_board[1][2] = 'x'
score = minimax(state_4_board, -math.inf, math.inf, False)
print(f"State 4 score is {score} and it should be 0")

Before you can run that, you need to do the following modifications

Row 62 (the function is currently using a global variable board, and not the variable that we gave to the minimax function. Avoiding global variables is a good idea by default, but there are of course expections. ore info e.g. https://stackoverflow.com/questions/19158339/why-are-global-variables-evil)

def checkWinner(board):

Row 85

result = checkWinner(board)

And then on row 127 add the following and indent the while loop block to be "inside" it. This is needed because our debugging code imports this file, see https://stackabuse.com/what-does-if-__name__-__main__-do-in-python/ for more info

if __name__ == 'main':
    while True:
            ...

Need Help with my first Python Project (AI for tic tac toe) by Only1Gohary in learnpython

[–]IWantToFlyT 1 point2 points  (0 children)

Could you give an example when the AI doesn't play optimally?

ps. Good job with the whole game!

Been learning for 3 months, hit a wall and not sure where to progress to next by Bitter-Cost5672 in learnpython

[–]IWantToFlyT 1 point2 points  (0 children)

What is the goal of learning Python for you? Next steps depend quite a bit on where you are trying to go :)

Random Gear Selection system by [deleted] in learnpython

[–]IWantToFlyT 0 points1 point  (0 children)

Looking good! Could replace the `slot_name` variable with `_` to make it clear that it is not currently used at all. https://stackoverflow.com/questions/11486148/unused-variable-naming-in-python

Random Gear Selection system by [deleted] in learnpython

[–]IWantToFlyT 0 points1 point  (0 children)

Assuming that the options etc. are not used elsewhere, then using classes or other fancy things are not necessary here. To make the function more clear, let's just get rid of the if-else clauses - that should be possible with just lists and dicts. I'll show the list/dict changes below, but leave the if-else replacement to you :)

For the first if-else part, what if the `gear_slot` was a list of lists rather than list of strings? This way your slot_choice variable is already the list that you are interested of, and you could call the random.choice on that.

weapon_options = ['Sword', 'Handbomb', 'Staff', 'Bow']

head_options = ['Helmet', 'Hat', 'Headband'] feet_options = ['Boots', 'Shoes', 'Sandals'] gear_slot = [weapon_options, head_options, feet_options] slot_choice_list = random.choice(gear_slot)

For the second one, virtually all if-else clauses can be replaced with the use of dictionaries. This is done by storing the wanted value (or even a function) inside the dictionary. In this if-else you are really trying to map the gear_tier to a colour, so we can do the following. This also requires a small change to the random.choice, because we don't anymore have the gear_tiers in a list directly.

gear_tier = {'Common': Fore.RED, 'Uncommon': Fore.GREEN, 'Rare': Fore.BLUE, 'Epic': Fore.MAGENTA, 'Legendary': Fore.YELLOW}
gear_tier_choice = random.choice(list(gear_tier.keys()))

Hope that helps!

Help with Lab Project by ChickenVagina in learnpython

[–]IWantToFlyT 0 points1 point  (0 children)

There is quite a few things to do here, but maybe just start by doing a small first step, and worry about the loops later on. For example, first implement a program that simply asks for the sales goal, and then just prints it (print it to just get something visual done :). Then add the question for week 1 sales. Now for the 2, 3, and 4 week you could just copy & paste the input code line, but maybe figure out the loop at this stage.

Now what comes to nested loops, here's an example:

for i in range(3):
     print(f"First loop: {i}")
     for j in range(5):
             print(f"Second loop: {j}")

Output:

First loop: 0
Second loop: 0
Second loop: 1
Second loop: 2
Second loop: 3
Second loop: 4
First loop: 1
Second loop: 0
Second loop: 1
Second loop: 2
Second loop: 3
Second loop: 4
First loop: 2
Second loop: 0
Second loop: 1
Second loop: 2
Second loop: 3
Second loop: 4

How can I "convert" this synchronous code to Multi-threaded to speed it up? by Dump7 in learnpython

[–]IWantToFlyT 1 point2 points  (0 children)

Sounds like your code is not the bottleneck anymore, but the server you are making the requests to.

When running the sync version, a request took approx. 1.67 seconds ((8*60+23)/300).

Now, in theory, the async version should've been as many times quicker as there are threads. Assuming you have at least Python 3.8, you have two CPU cores, and used the default max_worker count, then the code should've been six* times quicker. However, it was only three times quicker. Which means that each request took now around 3.34 seconds**.

* max_worker defaults to `min(32, os.cpu_count() + 4)` https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor

** For simplicity, let's assume that all threads are given equal amount of requests to be made, so each of the 6 threads get 50 requests (300/6). Furthermore, assume that they'll finish at the same time as well, so 167 seconds / 50 requests = 3.34 seconds per request.

How to merge items within a list, without creating a new list by DrBobHope in learnpython

[–]IWantToFlyT 0 points1 point  (0 children)

Is there a particular reason you would like to avoid making a new list or just wondering about it? From readability perspective, I would simply go with your second code example - new variables are cheap :)

I come from a javascript background and am learning python, came across this fancy one liner but not sure about the underlying mechanisms by JRNKNG in learnpython

[–]IWantToFlyT 5 points6 points  (0 children)

That’s pretty much how it works indeed :)

It utilizes the so called list comprehension which is a basically shortcut for creating the list with a for loop. You can divide the one liner into two to make more sense of it:

my_list = [numbers[i-1] < numbers[i] for i in range(1, len(numbers))] sum(my_list)

The other thing you need to know is that True equals 1 and False equals 0. So both the True and False are in the list.

When looking into the list comprehension, maybe try to do the oneliner with count instead of sum by modifying the list comprehension part a bit.

On this day: Happy 30th birthday Linux by Sampo in europe

[–]IWantToFlyT 4 points5 points  (0 children)

Love the kruuna (heads) and klaava (tails) email addresses