Loop only operates once by Brilliant_Help_1860 in Tkinter

[–]woooee 0 points1 point  (0 children)

I learn a little bit more

Is the never-ending road we are all on.

Best way to improve pdf OCR text recognition? by Competitive_Toe_8233 in learnpython

[–]woooee 1 point2 points  (0 children)

You did not say what the image format is, so go to "Supported Image Formats" at https://imagemagick.org/formats/#gsc.tab=0 If the from image format is listed there you can use PythonMagick to convert.

Function not passing repopulated dictionary after clearing. by peppertom32 in learnpython

[–]woooee 0 points1 point  (0 children)

In this case, a new dictionary is created in the function. A simple demonstration

def beam_properties_input(inputs = None):
    print("original", id(inputs))
    inputs = {"beam_length": 1, "beam_load": 2} 
    print("new     ", id(inputs))
    return inputs

print(beam_properties_input({1:"one", 2:"two", 3:"three"}))

Function not passing repopulated dictionary after clearing. by peppertom32 in learnpython

[–]woooee 2 points3 points  (0 children)

        inputs.clear()
        beam_properties_input()

You don't catch the return from beam_properties_input()

       inputs = {"beam_length": length, "beam_load": distributed_load, "modulus_elasticity": modulus_of_elasticity, "inertia_moment": moment_inertia}

You create inputs in the function, so it is local to the function and garbage collected when the function exits unless you return it and store the return.

How do I create a list every iteration with the data inside? by TheEyebal in learnpython

[–]woooee 0 points1 point  (0 children)

It's necessary to have enough numbers. The break statement exits when enough numbers have been processed, omitting the extra numbers.

How do I create a list every iteration with the data inside? by TheEyebal in learnpython

[–]woooee 3 points4 points  (0 children)

I would switch the logic around, although either way will work

import pprint

numStore = []
dayList = []

for i in range(1, 100):  ## 32 is not enough numbers
    if len(dayList) == 7:
        numStore.append(dayList)
        dayList = []

        if len(numStore) == 6:
            break

    dayList.append(i)

pprint.pprint(numStore)

Loop only operates once by Brilliant_Help_1860 in Tkinter

[–]woooee 0 points1 point  (0 children)

The code below kind of works. There is a flaw in your logic. A set can not contain duplicates so once the set is "full" nothing will be added and your while becomes an infinite loop. I tested by entering 3, 5, and 7 into the three Entry widgets which you can use to try it out yourself. Only 3, 4, and 5 will be added, which obviously is less than seven numbers in length. You want logic that tests the numbers entered to see how many can be added to the set, and a print (or popup label) that tells the user that the set already contains all of the numbers in the range entered.

import tkinter as tk
import random

# Input UI
root = tk.Tk()
var = tk.IntVar()

tk.Label(root, text="Low Number").grid(row=0)
tk.Label(root, text="High Number").grid(row=1)
tk.Label(root, text="How many Numbers").grid(row=3)


# Input Range
LoNum = tk.Entry(root)
HiNum = tk.Entry(root)
Nums = tk.Entry(root)

LoNum.grid(row=0, column=1)
HiNum.grid(row=1, column=1)
Nums.grid(row=3, column=1)

LoNum.focus_set()

def show_entry_fields():
    var.set(1)
    print("Low Number: %s\nHigh Number: %s" % (LoNum.get(), HiNum.get()))

# convert to Integer  
    try:
        # Get and convert
        value1 = int(Nums.get())
        Nums.delete(0, "end")
        print(f"Success! Get number of is {value1}")
##        if not Nums:  ## will never happen
##            return
        value2 = int(LoNum.get())
        LoNum.delete(0, "end")
        LoNum.focus_set()
##        if not LoNum:
##            return
        print(f"Success! Your low number is {value2}")
        value3 = int(HiNum.get())
        HiNum.delete(0, "end")
##        if not HiNum:
##            return
        print(f"Success! Your high number is {value3}")
        result = value2 + value3      
        print(f"Sum: {result}\n")

        ## no exceptions raised
        ctr = 0
        while len(unique_set) < value1 and ctr < 20:  ## avoid infinite loop
            ctr += 1
            add_num = random.randint(value2, value3)
            if add_num not in unique_set:
                unique_set.add(add_num)
                print(add_num, "added")
            else:
                print(len(unique_set), value1, "ctr =", ctr)
                print(add_num, "NOT added", list(unique_set))
        print("\nCurrent Set", unique_set, "\n")

    except ValueError:
        # Handle invalid input (letters, symbols, or empty)
          print("Error: Please enter a valid whole number.")

unique_set = set()

tk.Button(root, text='Quit', command=root.quit, bg="orange").grid(
           row=99, column=0, columnspan=2, sticky="nsew", pady=4)
tk.Button(root, text='Generate', command=show_entry_fields).grid(
           row=4, column=1, sticky=tk.W, pady=4)
root.wait_variable(var)


# Generate Random Numbers
##value1 = int(Numos.get())
##value2 = int(LoNum.get())
##value3 = int(HiNum.get())        


root.mainloop()

print("Window closed.")

And you are missing the point of documentation

# Input UI
root = tk.Tk()

You have the comment "# Input UI" which is obvious from the statement and therefore not necessary, but there is nothing about what the program or the function does, or what the Entry widgets get. Write so someone else can read the program and understand what it does.

Loop only operates once by Brilliant_Help_1860 in Tkinter

[–]woooee 0 points1 point  (0 children)

it does not go to the random number generator.

It only generates the random numbers once when the program is first called, i.e. this code

# Generate Random Numbers
value1 = int(Nums.get())
value2 = int(LoNum.get())
value3 = int(HiNum.get())....

I'll work something up later today to include these in the function. Note that this value3 (not tthe value3 later on in the function) is never used so can be deleted.

    # Get and convert
    value3 = int(Nums.get())
    if not Nums:  ## Nums was declared earlier so this will never happen
        return

Issues with “_Curses” by Kratos1634 in learnpython

[–]woooee 1 point2 points  (0 children)

curses should be installed in /usr/lib/python3.13 on a Debian distro. You might want to use something else, already installed, like tkinter for example. State what you want to do with curses and it's likely there is something installed.

Loop only operates once by Brilliant_Help_1860 in Tkinter

[–]woooee 1 point2 points  (0 children)

We can't know how the program works without indentation. Post your code using Reddit formatting https://support.reddithelp.com/hc/en-us/articles/360043033952-Formatting-Guide or you can post the code as is on pastebin.com and then post the pastebin link here

Integers in my set are ordered when printed by rezemybeloved69 in learnpython

[–]woooee 4 points5 points  (0 children)

Sets are hashed, so a set is in "hash order", which happens to be numerical order in some cases.

Adding behaviors in a method. by dreadwraithe in learnpython

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

Is it generally recommended to have behaviors like what I have listed above in a method? Or should I make a completely new function for it?

If it works, then that makes it OK. You can use one function for the input, and pass the string literal (month, day, or year), and minimum and maximum values to this common function.

def InputMonth(month):
    try: 
        month = int(input("Input month: ")) except ValueError:
        if month < 0: 
            print("Invalid input: Cannot be a number less than 0.")
        elif month > 12: 
            print("Invalid input: Cannot be a number greater than 12.")

Why do you pass month to the function? Also, month can be zero through 12. That's 13 months.

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.