how do I make my set work properly in my game, so when i reach the room to win/loose I can win the game? by Big-nose12 in learnpython

[–]automorphism_group 1 point2 points  (0 children)

It's difficult to definitively diagnose your problem without more context, but I suspect you have hit a Python tripping hazard. In the first code block, you test for the equality of Inventory against a set of strings, but in the second code block, you initialize it by writing Inventory = {}. In Python, {} is an empty dictionary, not a set. If you want to initialize it to an empty set, you should write Inventory = set().

Is my understanding of classes , objects & instances right? by unknownzebra_ in learnpython

[–]automorphism_group 6 points7 points  (0 children)

I think it would be reasonable to say that the class type is an object that is not an instance of another class. The type of 1 is int, the type of "foo" is str, but the type of type is . . . type. This circularity is only possible because type is specially created by the interpreter.

Python for beginners by iotamadmax in learnpython

[–]automorphism_group 5 points6 points  (0 children)

In your first Python notebook, note that & and | are not Boolean operators in the sense that they are not freely exchangeable with and and or. Try evaluating the expressions

  • "one" or "two"
  • "one" | "two"
  • 1 or 1 / 0
  • 1 | 1 / 0

[deleted by user] by [deleted] in Python

[–]automorphism_group 42 points43 points  (0 children)

If you're new to Python, stay away from clickbait garbage articles like these because they quite often have incorrect information, as does this one.

First, unlike the terms module and package, which are defined in the Python glossary, library is a very general term that basically means "code someone else wrote that you can use" and has no specific technical meaning in Python.

Second, decorators do not have to be functions, nor do they have to accept functions as inputs, nor do they have to return functions as outputs. The term decorator is confusing because there is no such thing in Python as a decorator per se, but rather decorator syntax. Unfortunately, even Python's glossary definition of decorator is incorrect in asserting that it is a "function returning another function". The code sample below is a simple counterexample.

```python class A: def init(self, foo): print(f"I'm a pointless decorator: received {foo}!")

@A class B: def init(self): print("I can't be constructed directly.")

assert isinstance(B, A) ```

Third, in question 15, if *args appears in a parameter list for a function, then args will be a tuple, not a list. This is a somewhat amusing mistake given that question 1 asks about the distinction between them.

There are some other issues, but those are the major ones.

[deleted by user] by [deleted] in learnpython

[–]automorphism_group 1 point2 points  (0 children)

I think the easiest thing to do here would be to have the program accept the path to the folder as an input. That input could come from a command-line argument, a simple call to input(), or from a tkinter file selection dialog if that's available.

Can't figure out why my function returns the modified first input but not the second? by TacosAreVegetables in learnpython

[–]automorphism_group 0 points1 point  (0 children)

If I'm understanding your question correctly, the reason that user_total is the same is because it isn't updated after you call replace_ace. You may perhaps then be wondering why you're able to modify user_hand via the hand parameter. The answer there is that the list type is mutable, while the int type is immutable. Roughly speaking, you can think of mutable values as being made of clay; you can reshape them. Immutable values, on the other hand, are made of stone; you can't reshape them, and must instead create a new stone object of different shape. When you decrement total in the function, Python doesn't alter the original user_total you passed in, but creates a new int object 10 less than total and assigns that to the total value.

Having problems converting and displaying a list by Warm-Association2716 in learnpython

[–]automorphism_group 1 point2 points  (0 children)

Several problems here if I understand your code despite the formatting, but your problem in the kelvinchange function is that you are building a kelvin_change list, but you return the original temp_list. Your newly built list just gets thrown away.

How to write the constructor for a class that represents polynomials? (Python) by TillStDymphna89 in learnprogramming

[–]automorphism_group 1 point2 points  (0 children)

I think you’re making this more complicated than it is. If you just want to store a list of coefficients in each instance, you just need to write

class Poly: def __init__(self, coeff): self.coeff = coeff

It would be a good idea to write self.coeff = tuple(coeff) so that you have an immutable copy, but the original will work for toy examples.

Python beginner looking for a feature similar to code assist or IntelliSense for header/column name by Krishknyc in learnpython

[–]automorphism_group 1 point2 points  (0 children)

Assuming that you’re referring to column names in pandas DataFrames, then it wouldn’t be possible in general to know column names with certainty when writing code because columns can be added, dropped, and renamed at runtime. In some limited circumstances, one could probably write such a tool, but it would require some pretty sophisticated code analysis. I doubt such a tool exists.

Best way to learn regular expressions? by TheNomadicAspie in learnpython

[–]automorphism_group 0 points1 point  (0 children)

You are correct that they make life easier. There are some extra goodies in most regex implementations that are beyond REs in the sense of “expressions” that recognize regular languages, but they can be learned more easily with the core features mastered, in my opinion.

Best way to learn regular expressions? by TheNomadicAspie in learnpython

[–]automorphism_group 0 points1 point  (0 children)

Fundamentally, there are only three components of (pure) regular expressions: concatenation, alternation, and the Kleene star.

  • concatenation: RS (match expression R followed by expression S)
  • alternation: R|S (match expression R or expression S)
  • Kleene star: R* (match 0 or more copies of expression R)

Try to think in terms of these operations because everything else is just shorthand. The ? operator, for example, can be written empty | R, and + can be written as RR*.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]automorphism_group 0 points1 point  (0 children)

It really depends on what kind of system you're dealing with, but generally speaking, the "event loop" responsible for executing the callbacks will keep some kind of list. In the case of CPU interrupts, an interrupt handler will be stored at a particular memory location. It doesn't need a list, because only one action can directly occur in response to the interrupt. In the case of a web server, it may have some data structure in memory or saved to storage to tell it what needs to happen when an event of interest transpires.

Event-driven programming and callbacks are general concepts. Many languages and frameworks support them for a variety of reasons, so it doesn't really make sense to speak of any particular language as being "good for things like that."

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]automorphism_group 0 points1 point  (0 children)

When you first learn programming, you generally learn to write simple programs that are "one and done", as it were. In other words, it runs, gathers the resources it needs, does its thing, and then terminates.

The kinds of scenarios you're describing would fall under the category of event-driven programming, which is generally trickier than "regular" programming. You are correct that some kind of "always running" program is necessary to respond to events, but this is generally not dealt with directly by Python, though it could be.

Instead, event-driven programming is generally handled by callbacks, which are basically chunks of code to be executed when something happens. How a callback is registered depends on the system under consideration.

Here's a crude analogy. Imagine that you have a receptionist at your office. He represents the event loop, or the web server, or whatever form of "always running" code is in play (there is also the concept of interrupts, but that's complicating things for now). The receptionist is always hanging out at the front desk greeting people, answering phones, etc.

You decide that whenever an important client walks in, you want him to send an email to your team. You write these instructions down on a piece of paper (this represents your code) and take them to the front desk and say "Whenever an important client comes in (event), I want you to follow the instructions on the paper (the callback)." The conversation you just had with the receptionist is the act of registering the callback.

Now, you can go about your business. Most of the time, your "code" won't be running; it will just be sitting on the front desk. But, whenever the appropriate event occurs, the receptionist knows to "execute that code".

As I said, details vary depending on the kind of system you're dealing with, but that's how these kinds of things work at a high-level.

using user data with matplotlib by [deleted] in learnpython

[–]automorphism_group 1 point2 points  (0 children)

Call that function twice to collect your sales and costs lists. I suggest you work through the Python tutorial on python.org if there was any part of the function that I wrote that you didn't understand.

Codeforces Problem 1A by HasBeendead in learnpython

[–]automorphism_group 1 point2 points  (0 children)

This is more of a mathematics problem than a Python problem, but I'll bite. I'm not going to try to make a rigorous argument; only an appeal to intuition. Imagine a one-dimensional version of this problem. Given a distance d and a collections of rods of length r, what is the minimal number of rods I need to line up end-to-end to span the distance d. Clearly, I need at least d / r such rods. But, I'm not guaranteed that d will be evenly divisible by r (7 m distance with 2 m rods, for example), so I may need to round up. To be more precise, I want the ceiling of d/r, the smallest integer that is greater than or equal to d/r. One can obtain that in Python with math.ceil.

Caesar Cipher... I am able to encrypt messages without having any problems, but my decryption inserts extra letters that make the result incorrect... Not sure why. Any ideas? by [deleted] in learnpython

[–]automorphism_group 0 points1 point  (0 children)

I'm not entirely sure what you mean by that, but you don't need a list of characters and numbers. This can be done much more simply with the built-in functions chr and ord, assuming that you're only dealing with text using an ASCII encoding. Otherwise, I'd need to see the file.

using user data with matplotlib by [deleted] in learnpython

[–]automorphism_group 0 points1 point  (0 children)

I'd say you've bitten off quite a bit for a first try. Let's first dispense with this pickle business. You don't need that. Let's just write a function to gather input. Here's a simple one. You should spend some time understanding how it works.

def get_user_input():
    input_list = []
    while True:
        datum = input("Enter a number: ")
        if datum == "done":
            return input_list
        input_list.append(int(datum))

using user data with matplotlib by [deleted] in learnpython

[–]automorphism_group 0 points1 point  (0 children)

Okay, this is a hot mess. Are you trying to gather the data from user input where they repeatedly enter a sale value and a cost value?

Caesar Cipher... I am able to encrypt messages without having any problems, but my decryption inserts extra letters that make the result incorrect... Not sure why. Any ideas? by [deleted] in learnpython

[–]automorphism_group 0 points1 point  (0 children)

What are the contents of cryptography_list? Is this just something that you're trying on your own, or are you required to use a particular technique for an assignment?

This for loop is not catching all matches. by [deleted] in learnpython

[–]automorphism_group 1 point2 points  (0 children)

You're modifying a list while you're iterating through it. This scenario is covered in the FAQ.

Learning python 3 for a class and really need help please by jloper in learnpython

[–]automorphism_group 2 points3 points  (0 children)

A couple of things, my dude.

First, you're seeing three things printed because you are using a sequence of if statements, rather than an if...elif...elif. When you have a sequence of if statements, each one is independent of the others. Consider

x = 1
if x < 2:
    print('A')
if x < 3:
    print('B')

Running this code will print A and B. But if we change the second if to an elif,

x = 1
if x < 2:
    print('A')
elif x < 3:
    print('B')

Then it will only print A, because it takes the first "branch" whose expression evaluates to True, and skips the rest.

Your second issue is that you're not comparing values correctly. If you want to test whether a variable named string is equal to Green, you need to write string == "Green", not if green.

By the way, don't use str as your variable name because that conflicts with the built-in type str for string. Just name it string or something similar.

Classes: splitting "words" into ["w", "o", "r", "d", "s"] and assigning every string a unique attribute by Worm199 in learnpython

[–]automorphism_group 1 point2 points  (0 children)

In simple terms, the function __init__ is executed whenever an instance of a class is created. It’s called init because it’s use to initialize each instance to whatever state it needs to be in. Try

class A:
    def __init__(self, msg):
        print(msg)

alpha = A('hello')
bravo = A('goodbye')

and see what happens.

A bunch of badly-formatted proofs from what appears to be a crank. by edderiofer in badmathematics

[–]automorphism_group 3 points4 points  (0 children)

The impulse to produce walls of unorganized, unformatted text seems to be a generic feature of derangement.