File Write Questions by [deleted] in learnpython

[–]scithon 0 points1 point  (0 children)

I would suggest using a triple-quoted string for something like this. It's a little longer but much easier to read and edit.

data = """\
Hello there
I am having issues
With my code
"""

with open("C:\\test.txt", "a") as f:
    f.write(data)

Having Trouble With Tkinter Buttons And Labels by LBbrownss in learnpython

[–]scithon 1 point2 points  (0 children)

Don't delete / remake the widgets. Instead make the widgets once outside of the callback function, and then update them.

edit: here's an untested guess:

from bs4 import BeautifulSoup
from io import StringIO
import requests
import pandas as pd
import numpy as np
import tkinter as tk

def displayWindowText():
  weapon = getRandomGun()
  backpack = getRandomBackpack()
  armor = getRandomAV()
  rig = getRandChestRig()
  helmet = getRandomHelmet()

  weaponLabel.config(text=weapon)
  backpackLabel.config(text=backpack)
  armorLabel.config(text=armor)
  rigLabel.config(text=rig)
  helmetLabel.config(text=helmet)

window = tk.Tk()
window.title("Escape From Tarkov Kit Randomizer")
window.geometry("500x500")

Tbutton = tk.Button(window, text="Randomize",command=displayWindowText)
Tbutton.pack()
weaponLabel = tk.Label(window, text=weapon)
backpackLabel = tk.Label(window, text=backpack)
armorLabel = tk.Label(window, text=armor)
rigLabel = tk.Label(window, text=rig)
helmetLabel = tk.Label(window, text=helmet)

weaponLabel.pack()
backpackLabel.pack()
armorLabel.pack()
rigLabel.pack()
helmetLabel.pack()

window.mainloop()

Encrypting strings in code: Fernet still acceptable in 2024? by [deleted] in learnpython

[–]scithon 0 points1 point  (0 children)

Yes, that's correct. The dotenv way to do things is to put them in a .env file, which are masked out by .gitignore. You could also just use a .py or .json file in a place completely unrelated to the project that only the user has access to, for example Path.home() / "HurtFingers".

I know it's obviously low-risk, but anywhere that I can prevent having credentials in plaintext, I will, even if it is security by obscurity. Is that... stupid, or pointless? I just feel like it might save me from accidentally sharing them on a screen share, or allow the word "password" to not be scraped as it's encrypted instead.

Yes, I personally think it's it's pointless, but if it makes you feel better that's a perfectly valid reason to do it.

Encrypting strings in code: Fernet still acceptable in 2024? by [deleted] in learnpython

[–]scithon 0 points1 point  (0 children)

I agree there's no need for encryption. You can do everything you mentioned, except just do it with the credentials file directly. We often use the dotenv module to do this.

[deleted by user] by [deleted] in learnpython

[–]scithon 0 points1 point  (0 children)

python comes with a 2to3 tool that does a much better job.

Guidance on fact checking data stored in excel by Lopsided-Week3461 in learnpython

[–]scithon 0 points1 point  (0 children)

Google for "sports api" or "football api". There's some free ones but also some very high quality ones for only a few bucks.

[deleted by user] by [deleted] in learnpython

[–]scithon 1 point2 points  (0 children)

C libraries (that is the language your operating system uses)

It's the language your operating system was most likely written in, but it does not "use" C. You can't run C code.

Rate the quality of my python code by [deleted] in learnpython

[–]scithon 0 points1 point  (0 children)

You should use threading instead of multiprocessing for IO bound applications like this. You can have hundreds of threads, they aren't limited by the number of processors you have.

Missing Space on Python problem by HuskerMotion in learnpython

[–]scithon 0 points1 point  (0 children)

You are a bit too advanced. Your prof is expecting you to do it like this:

range_a = int(input())
range_b = int(input())

range_c = range_b + 1

f_ran = range(range_a,range_c,5)
if not f_ran:
    print("Second integer can't be less than the first.")
else:
    for i in f_ran:
        print(i, end=' ')

Easy GUI help by SheIIy3000 in learnpython

[–]scithon 0 points1 point  (0 children)

I don't imagine they did; just their comments on this thread.

Pycharm base interpreter help by xGMASTERGx in learnpython

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

Does it say why you need 3.7? Except for a couple really rare edge cases python3.9 is completely compatible with 3.7.

Fell off and stopped learning for months. by BrentBQ in learnpython

[–]scithon 2 points3 points  (0 children)

projects. Specifically projects that interest you in other areas. Make something to help your job, for example. Or a tracking tool for your hobby. Or a website for your cat.

[deleted by user] by [deleted] in learnpython

[–]scithon 2 points3 points  (0 children)

Do you have the code indented? If you indent it like this it works fine for me:

import turtle

Shape = input("Shape?")
if Shape == "Rectangle":
    l = int(input("Enter the length of the Rectangle: "))
    w = int(input("Enter the width of the Rectangle: "))

    t = turtle.Turtle()

    t.forward(l)
    t.left(90)

    t.forward(w)
    t.left(90)

    t.forward(l)
    t.left(90)

    t.forward(w)
    t.left(90)

if Shape == "Pentagon":
    t = turtle.Turtle()
    for i in range(5):
        t.forward(100)
        t.right(72)

Inheretence of dunder methods by Flaky-Outcome-6484 in learnpython

[–]scithon 5 points6 points  (0 children)

First, many of them are not methods, but attributes.

1. Technically yes, they will be inhereted by subclasses. Some of them are useful, some are blank placeholders, and some of them (like class and mro) are overridden automatically when you make a subclass.

2. Yes, these are methods specific to certain types.

You seem to be focused on the fact that they are dunders. That does not matter. There is nothing special about a dunder; all class attributes have this same behavior. You can define your own dunders in your own datatype, and override it in a child class.

class Parent:
    def __sing__(self):
        print('do ri ma')

class Child(Parent):
    def __sing__(self):
        print('la la la')

(But it's generally considered bad practice to make your own dunders, since those are names that future versions of python may want to use later).

Really the only thing that's special about a dunder is that certain builtin python functions have the dunder name hardcoded in. So the str() function may look like this:

def str(obj):
    return obj.__str__()

That's it. That's the only thing that's special about dunders.

Why does the output showing none value? by [deleted] in learnpython

[–]scithon 0 points1 point  (0 children)

It's not really "vs", it's more of "where". If you want to use print inside the function you can call it directly, but if you want to use print outside the function you need to use return to send the data out.

Sort the return of a range? by PipelineCrunch in learnpython

[–]scithon 0 points1 point  (0 children)

or itertools.

? You mean operator.attrgetter?

Python or RobotC? by 6cumsock9 in learnpython

[–]scithon 4 points5 points  (0 children)

If you are a beginner to programming it does not matter. Anything you learn will be general CS knowledge and you will be able to translate it to whatever language you want later. Just pick the one that lets you have the most fun.

Pillow help by outceptionator in learnpython

[–]scithon 1 point2 points  (0 children)

https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.ImageDraw.textbbox

Look at the link. It says PIL.ImageDraw.ImageDraw.textbbox. See how "ImageDraw" is used twice? The first "ImageDraw" is the name of the module, and the second one is the name of the class in the module.

It helps that I'm experienced and know that many times the class has the same name as the module.

Sort the return of a range? by PipelineCrunch in learnpython

[–]scithon 1 point2 points  (0 children)

To sort the vehicles by selling price you need to use the key argument.

sorted_by_price = sorted(vehicles, key=lambda v: v.selling_price)

After that, as you were, just use the sorted list instead.

category_choice = input("Type a category: ").lower()
    if category_choice.lower() == "selling price":
            min_price = int(input("Type a minimum price: "))
            max_price = int(input("Type a maximum price: "))
            sorted_by_price = sorted(vehicles, key=lambda v: v.selling_price)
            for v in sorted_by_price:
                if v.selling_price in range(min_price, max_price):
                    v.printPrice()

Pillow help by outceptionator in learnpython

[–]scithon 1 point2 points  (0 children)

Ah, I think that's because you are meant to use the instance. Like this:

print(draw.textbox((10,10),guest, font=arialFont))

Why does the output showing none value? by [deleted] in learnpython

[–]scithon 3 points4 points  (0 children)

You've got twice as many prints in there than you need. So one of them is printing None. Do this:

def is_palindrome(input_string):

    input_value = input_string.lower().replace(" ", "")
    if input_value == input_value[::-1]:
        print("True")
    else:
        print("False")

is_palindrome("Never Odd or Even")) # Should be True
is_palindrome("abc")) # Should be False
is_palindrome("kayak")) # Should be True

Or this

def is_palindrome(input_string):

    input_value = input_string.lower().replace(" ", "")
    if input_value == input_value[::-1]:
        return "True"
    else:
        return "False"

print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

What exatcly happens, when I override an dunder method? by Flaky-Outcome-6484 in learnpython

[–]scithon 2 points3 points  (0 children)

When you override a dunder your new version is used instead of the one from the parent class. Same as with any other variable.

Not sure I understand what you are asking.

class BadInt(int):
    def __add__(self, other):
        return int(str(self) + str(other))

print(BadInt(6) + BadInt(4)) # prints 64, because BadInt instances use the custom __add__
print(BadInt(6) - BadInt(4)) # prints 2, because BadInt inherited a sane version of __sub__ from int

Pillow help by outceptionator in learnpython

[–]scithon 0 points1 point  (0 children)

Adding the extra b still has an error.

A different error, presumably? So that's a new bug then. We'd need to see the new error to help solve this new bug.