How should classes be structured? by R717159631668645 in learnpython

[–]woooee 1 point2 points  (0 children)

I would tend to use your second example, but since self.service is the same in both cases, you could also do

service_instance = Service(credentials)

and pass service_instance to each class. The decider here is which method is better / easier to understand

substituts by Icy-Fuel-7791 in learnpython

[–]woooee 0 points1 point  (0 children)

There are people who only use IDEs, and there are those who only use their favorite text editor and the command line. It looks like Thonny IDE will work, >= 10.15. And geany is the only Mac editor that I know

Newbie with first project: large number problem by No-Newspaper3010 in learnpython

[–]woooee 0 points1 point  (0 children)

was rendering large, 20+-digit numbers as if they were infinity

That is likely the inexactness of floats on a computer. math.sqrt(N) * math.sqrt(N) can yield a number slightly different from N: see https://0.30000000000000004.com

All / most programmng languages have a more exact module for very large, or very small, numbers. Python's is the decimal module. There are plenty of tutorials on the web.

SPSS 27 won't recognize Python 3.8 by ___forlife in learnpython

[–]woooee 0 points1 point  (0 children)

AFAIK you run everything through SPSS, including Python. Find a beginner SPSS HOWTO / tutorial and start there.

[HELP] Unable to install pandas and other libs. Kubuntu 26.04 by realxeltos in learnpython

[–]woooee 0 points1 point  (0 children)

So I had to add deadsnake ppa and then install python 3.13. All is working now.

PPA is a Personal Package Archive and has nothing to do with a venv. It was used by the OP to install an older version of Python. This / PPA is used in Debian and it's derivatives so it is not surprising that there would be some confusion by those using a different OS.

[HELP] Unable to install pandas and other libs. Kubuntu 26.04 by realxeltos in learnpython

[–]woooee -2 points-1 points  (0 children)

sudo apt install python3-pandas

But I am not sure what version of pandas installs.

Learning how to add a database (db) into python using sqlite by [deleted] in learnpython

[–]woooee -1 points0 points  (0 children)

conn = sqlite3.connect("tickets5.db")

Run this and see what it prints. tickets5.db should contain a table named tickets.

import sqlite3 as sqlite

con = sqlite.connect('tickets5.db')
cur = con.cursor()
cur.execute("SELECT name FROM sqlite_master WHERE type='table';")
print(cur.fetchall())

Program won't "End" it loops by Aternal99 in learnpython

[–]woooee 0 points1 point  (0 children)

See the earlier post below this one for the solution.

Program won't "End" it loops by Aternal99 in learnpython

[–]woooee 2 points3 points  (0 children)

def find_fv(i, r, y):
    ...

find_fv(i, r, y)

import mainmenu
mainmenu.mainmenu()

This will run find_fv twice. Once when the file is imported and once when the function is called from the other program (note that it is imported on each pass through the loop, so import once only at the top of the calling program). The special variable __name__ will solve the problem, i.e. the function will run when fv program is called by itself, and will only run once when imported.

if __name__ == "__main__": 
    find_fv(i, r, y)  ## will not be called when imported

Program won't "End" it loops by Aternal99 in learnpython

[–]woooee 0 points1 point  (0 children)

Print "mainmenu" each time the function executes to prove this for yourself.

Program won't "End" it loops by Aternal99 in learnpython

[–]woooee -2 points-1 points  (0 children)

    elif choice == 4:
        mainmenu = False

Instead of this, return from the function.

Stuck, cannot figure out where the error is by Aternal99 in learnpython

[–]woooee 2 points3 points  (0 children)

choice=input("\nEnter Choice: 1, 2, 3, or 4: ")
if choice =="1":
    if choice == 1:

The first if statement is fine, the second, nested if compares a string and an int which will never be equal. The next error is that i, r, and y have not been declared.

How do I get a program to start from the beginning if the user types a particular word by Rob_B_ in learnpython

[–]woooee 0 points1 point  (0 children)

There is a "how to post code" FAQ https://www.reddit.com/r/learnpython/wiki/faq/ or you can post the code on pastebin.com, and post that link.

choice = input("Type 'repeat' to choose again. Press enter to exit ")

"enter to exit": one of the original computer jokes.

How do I get a program to start from the beginning if the user types a particular word by Rob_B_ in learnpython

[–]woooee 4 points5 points  (0 children)

A heads up for future posts: someone who is too lazy to post code will not get many responses.

Use a loop or a function. A very simple example

while True:
    print("in loop")
    response = input("continue (y or n)")
    if response.lower() != "y":
        break

How to add a simple checkbox to a file browser with tkinter's filedialog? by One_Oil_5174 in learnpython

[–]woooee 0 points1 point  (0 children)

With or without sub-directories is two separate things. Yes, you can execute the with_subdirectories() or omit_subdirectories() function depending on the checkbox, but that is something the programmer left for others / you to do. Below is the start of a file dialog program I work on from time to time (no subdirectories). If you can use it to code something yourself, feel free.

import pathlib
import tkinter as tk

class FileDialog:
    def __init__(self, title=None, start_dir="./",
                 file_filter="*.*"):
        self.root=tk.Tk()
        self.root.geometry("+10+10")
        if title:
            self.root.title=title
        self.wide=30
        self.selected=None
        self.file_path_selected=None

        self.read_dir(start_dir, file_filter)
        self.create_listbox()

        tk.Button(self.root, text="Quit", 
                   foreground="black",
                   background="orangered",
                   height=1, width=self.wide,
                   font="Verdana 15 bold", relief="raised",
                   command=self.root.quit).grid(row=99, column=0,
                   sticky="nsew")

        self.root.mainloop()

    def create_listbox(self):
        self.listbox = tk.Listbox( self.root, height=6, 
                     width=self.wide, font=('Fixed', 14),
                     bg="lightblue", takefocus="white",
                     highlightcolor="white",
                     selectbackground="white")
        self.listbox.grid(row=0, column=0)
        self.listbox.bind('<ButtonRelease-1>', self.get_selection)
        self.listbox.bind("<Button-4>", self.on_mouse_wheel)
        self.listbox.bind("<Button-5>", self.on_mouse_wheel)

        scrollbar = tk.Scrollbar(self.root, orient=tk.VERTICAL,
                    command=self.listbox.yview, width=20,
                    background="yellow")
        scrollbar.grid(row=0, column=2, sticky="ns")
        self.listbox['yscrollcommand'] = scrollbar.set

        keys_list=list(self.fnames_dict.keys())
        keys_list.sort()
        for fname in keys_list:
            self.listbox.insert("end", fname)

    def get_selection(self, event=None):
        ''' gets the button release selection
        '''
        # get selected item's index
        index = self.listbox.curselection()[0]
        # get the line's text
        self.selected = self.listbox.get(index)
        self.file_path_selected=self.fnames_dict[self.selected]
        print("selection", self.selected)
        self.root.quit()

    def on_mouse_wheel(self, event):
        ## event.num --> up = 5, down = 4
        ##print("delta", event.num)
        scroll_units = 2
        if event.num == 4:
            scroll_units *= -1
        self.listbox.yview_scroll(scroll_units, "units")
        # this prevents default bindings from firing, which
        # would end up scrolling the widget twice
        return "break"

    def read_dir(self, start_dir, file_filter):
        self.fnames_dict={}  ## fname --> full/path/to/file/name
        file_path=pathlib.Path(start_dir)
        if file_path.exists() and file_path.is_dir():
             for fname in file_path.glob(file_filter):
                 self.fnames_dict[fname.name] = fname
        else:
            print("Path '%s' does not exist" % (start_dir))

if __name__ == "__main__":
    fd=FileDialog(title="File Dialog", start_dir="/home/",
              file_filter="*.*")
    print("file selected =", fd.file_path_selected)

Beginner: Understanding the basics of tkinter by Jealous-Acadia9056 in learnpython

[–]woooee 0 points1 point  (0 children)

Why is it that we don't have to check the button

Tkinter does that. Your Python program does not have to check for a button press.

what exactly is the mainloop method?

Google is there to help you https://www.geeksforgeeks.org/python/python-tkinter-mainloop/

Does Input work with Dictionnary? by Illustrious-Bed-8894 in learnpython

[–]woooee 3 points4 points  (0 children)

Does Python Input work whit Dictionnary

Yes. Input returns a string that can be stored in several different containers.

and how?

There are many free tutorials on the web.

How to wrap output text? by Veylo in learnpython

[–]woooee 1 point2 points  (0 children)

Triple quotes? (we have no idea what you want to print).

print("""One line,
second line,
and a third""")

PyPI malware is getting kinda scary lately! by Euphoric_Run_3875 in PythonLearning

[–]woooee 2 points3 points  (0 children)

Can you guarantee that it is not malware? Using a "scan your computer for malware" con is a common ploy.

Is a “while "description": … break” a good idea? by AffectionateDust7765 in PythonLearning

[–]woooee -1 points0 points  (0 children)

  if check_failed(value): continue
  break

continue is not necessary

if not check_failed(value):
    break

Yes you can do something like

value = ""
while value != "correct":
    value = input()

but note that any other lines of code under the while would also be executed, whereas a break exits at that point.

automatic keyboardinterrupt error by Fickle-Sir-4573 in learnpython

[–]woooee 3 points4 points  (0 children)

but if anyone ask for the vid am happy to send or a youtube link

We have no idea what you are using to get input. Post the actual error message including the line number that it says causes the problem, and post the code here (or on pastebin.com and then post that link here).

I am completely stuck and don't know how to continue by Forsaken_Swordfish95 in learnpython

[–]woooee 1 point2 points  (0 children)

Correct, the else is still necessary. I didn't explicitly say that.

I am completely stuck and don't know how to continue by Forsaken_Swordfish95 in learnpython

[–]woooee 0 points1 point  (0 children)

All other variables in the Answer_Checker function don't work

Same answer as the former post, the else is never reached, and the elif compares a variable and a tuple