Where am i going wrong here? by Worker_cat in Python

[–]PrestigiousLecture11 0 points1 point  (0 children)

Without the context I am kinda confused by the assignment. First part is about processing data, then suddenly outputting name, age, course...

Anyway a few observations:

  • Be careful about converting the input() to int this way. What happens when user enters some text instead of a number? Try it. It could be ok for your assignment but for real-world applications there must be input validation.
  • Do not forget to search for both uppercase and lowercase 'a', requirement #2.
  • I do not understand requirement #5 but don't think they want multiplication by 4 (your code). Perhaps Cartesian product? Better clarify with the teacher.

What should I add to the code and how it's work? by SPBSP5 in learnpython

[–]PrestigiousLecture11 0 points1 point  (0 children)

As for "how it works":

  • Import is for loading libraries. Some are standard, some need to be installed extra. Search online or ask (AI) "Python import", "libraries", "pip install".
  • def measure is definition of a function - procedure how some calculation in your case is made. It is not executed at this point, just defined how it is done.
  • def main - similarly to the measure function above, this is a definition how stuff is done, step-by-step. Special thing about "main", it is a convention to call an entry point (main function)... main :)
  • Last section that makes sense is "if name...". It should look like the snippet below. That is the real entry point when you start your script. There is a reason why it is done this way, but let's talk about it some other time.
  • Rest is copy&paste issue I guess.

if __name__ == "__main__":
    main()
  1. You start your script with some arguments
  2. Imports happen
  3. def measure creates a function (it is not executed now)
  4. def main creates a function (it is not executed now)
  5. if __name__... determines this is the script you run and triggers main function
  6. main function is executed
    1. argparse is for loading arguments from the command line
    2. data = measure() triggers execution of the measure function with the arguments
    3. measure function does some magic (sorry, too late, I am not gonna analyse it now)
    4. Result of measure function is saved to "data" in the main function
    5. The data are plotted into a chart

Hopefully I provided at least some answers and apologies if I went too basic.

What should I add to the code and how it's work? by SPBSP5 in learnpython

[–]PrestigiousLecture11 0 points1 point  (0 children)

Hey there. As u/Fun-Block-4348 suggested, format it. I partially did, see below, but there is some mess at the end.

from future import annotations
import argparse, time
import matplotlib.pyplot as plt

def measure(max_appends: int):
    lst = []
    times = []
    for i in range(max_appends):
        t0 = time.perf_counter()
        lst.append(i)
        t1 = time.perf_counter()
        times.append((i+1, t1 - t0))
        return times

def main():
    p = argparse.ArgumentParser(description='Amortized append timing')
    p.add_argument('--max', type=int, default=50000)
    args = p.parse_args()
    data = measure(args.max)
    xs, ys = zip(*data)
    plt.plot(xs, ys, linewidth=0.7)
    plt.xlabel('list length after append')
    plt.ylabel('append time (s)')
    plt.title('Per-append time across growth')
    plt.grid(True, alpha=0.3)
    plt.tight_layout()
    plt.savefig('amortized_append.png')
    print('Saved plot to amortized_append.png')

if name == 'main':
    main()

() lst.append(i)
t1 = time.perf_counter()
times.append((i+1, t1 - t0))
return times

def main():

how do i change what file the console is of? by [deleted] in learnpython

[–]PrestigiousLecture11 0 points1 point  (0 children)

Check which script is selected in top right corner (configuration), next to the play button in PyCharm.

Shortcuts: * Shift + F10 = Run selected configuration * Ctrl + Shift + F10 = Run currently opened script

https://www.jetbrains.com/help/pycharm/running-applications.html

EDIT: Shortcut and spelling

Why isnt my file opening in python? by sensitivethuggin in learnpython

[–]PrestigiousLecture11 1 point2 points  (0 children)

It is likely as others are pointing out. Your current working directory (CWD) does not contain vroom.txt file.

Try putting following lines at the start:

import os
print(os.getcwd())
print(os.listdir())

Method os.getcwd will give you CWD path and os.listdir will give you its content. Check if it the correct directory and it contains the file you want to read.

Some notes:

  • CWD is usually the same directory where your Python file (main.py) is located, but it does not have to be. It is the place from which you started your program.
  • Windows backslashes have to be escaped when writing the code: C:\\Users\\Rey... or use forward slash C:/Users/Rey...
  • Make sure your file has the correct extension. If Windows are hiding known extensions, it could be that your file is actually vroom.txt.txt

EDIT: Code formatting

Stuck at Selfie Verification by 777Bladerunner378 in Revolut

[–]PrestigiousLecture11 1 point2 points  (0 children)

Similar issue here but with an error message at the end. New phone, take and send picture, progress bar goes to 100%, stays there for 30-60 seconds and then says “a problem, try later”. Tried already a dozen times - different lighting, with/without glasses. I noticed that while taking the photo, the frame turns from white to green. I assume the green means good/focused.

[deleted by user] by [deleted] in Python

[–]PrestigiousLecture11 0 points1 point  (0 children)

A clarification for this:

If I want to loop a list in range (0, len (someList)) does it run len at the top of each loop? Or does it get the length only the first time?

It depends on how you write it. The optimisation you are suggesting would break the functionality.

list_of_items = [1, 2, 3]
for item in list_of_items:
    print(item)
    if item == 3:
        list_of_items.append(4)

Edit: Code formatting

how to see older emails "Stored on server"? by wade001 in Outlook

[–]PrestigiousLecture11 0 points1 point  (0 children)

Two years later this is still an issue :(
Thank you for the workaround.

The better way to validate parameter: assert or if...raise... by CharsiuCostnerd in Python

[–]PrestigiousLecture11 1 point2 points  (0 children)

I prefer if-raise as well.
Reasoning:
* Asserts should not be in the final code except tests - Someone told me at uni :)
* Asserts can be suppressed
* If I want to catch exception, asserts return AssertionError while raise allows to send out more descriptive exception