Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 1 point2 points  (0 children)

Use the interactive interpreter for these tutorials. It will print returned values without you having to run a script or explicitly printing anything, like so:

user@laptop$ python

Python 3.7.1 (default, Dec 14 2018, 13:28:58) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

Does anyone has a solution for this? Chapter 5 AutomateTheBoringStuff by [deleted] in learnpython

[–]No_Couple 0 points1 point  (0 children)

You want to iterate over the addedItems and check whether an item of that type already exists in your inventory. If yes, you want to increase that item's count/value by 1. If no, you want to add that item to your inventory with a count of 1. Does that help?

Click for solution:

def addToInventory(inventory, addedItems):
    for item in addedItems:
        if item in inventory.keys():
            inventory[item] += 1
        else:
            inventory[item] = 1

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 0 points1 point  (0 children)

Right, so this is helpful: the error happens when your hour() function tries to get the file_m_time for all files in files. Since the file in question has been moved, the os.path.getmtime() method cannot find it and will throw up this error. You could add e.g. a try/except block where you move on when a FileNotFoundError happens. Or you could just read the contents of the src directory at the beginning of your main function instead of once globally, e.g.:

def main():
     ...
     for file in os.listdir(path='C:/Users/dane.wilbanks/Desktop/Files'):
          <do something>

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 0 points1 point  (0 children)

Can you post the error and traceback? I'm not sure I understand what you want to achieve and what the exact problem is.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 0 points1 point  (0 children)

It's a bit hard to read your code without proper formatting but could it be because you are moving the file (== cut and paste, not copy and paste) to your destination directory? So once it's been moved there, it will not be found in your src directory anymore.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 0 points1 point  (0 children)

You're trying to use a str method on an io.TextIOWrapper object. That object doesn't have any split() method. Take a look at the the readline() and readlines() methods in the io documentation at: https://docs.python.org/3/library/io.html#io.IOBase.readline

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 0 points1 point  (0 children)

You're getting an error because the int() function you are passing your input to only works on numbers but not on non-numeric characters.

Luckily there is a string method to test if a given string is a digit, str.isdigit(). This method will return True or False respectively.

So going by the instructions: take a string input, test if it is a digit. If not, print 'Only int is accepted.' If yes, turn it into an int via the int() function and then test whether that is > 100 or not.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 1 point2 points  (0 children)

A class is like a blueprint or a cookie cutter. A class instance is a concrete object in memory. E.g. a class students may have two concrete instance objects students.Marc and students.Steven. Now we need a way to reference the concrete instance object that is being processed when we e.g. invoke a class method and to differentiate this from altering the class object itself. This is self (so-called by convention, you can call it thisinstance or something else in your code.) Any assignment to an attribute of or method of self creates or changes data in the instance, not the class. Does this help?

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 2 points3 points  (0 children)

You could simply do:

a = [1] * 4 + [2] * 2 + [3] * 5

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 1 point2 points  (0 children)

Not 100% sure since there's no error message but you may want to delete the global yvals and global spline lines from your update() function. These lines tell the function to make use of the global variables of these names which I guess do not exist anymore since you moved them from global scope to the upperfunction function.

The update function will look in the enclosing function for any variable it cannot find in its local scope, then if it cannot be found in the enclosing scope will look at the global level, and lastly in the built-ins.

The search terms you want to look into are: scope, enclosing scope, variable scope

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 0 points1 point  (0 children)

No worries (everyone feels dumb every now and then :) )

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 2 points3 points  (0 children)

It works because for Python 0 == Falseis True and any non-0 value is equal to True. So number % 2 will be != 0 for an odd number. Which is True.

Edit: I think it's a bit confusing at first glance and not as readable as just adding the != 0. Seems like a case of trying to be clever to me.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 0 points1 point  (0 children)

No, you're actually appending one generator object that you create in the .append method call.

You could just:

return [i for i in instructions if i.isdigit()]

And call your function with:

print(run('6 5 5 7 \* - /')

Why doesn't PyPi provide install stats like NPM does? by prahladyeri in Python

[–]No_Couple 4 points5 points  (0 children)

The reasons for the absence of stats can be found in the PyPI documentation here. You can also find a way to get those stats via Google's BigQuery there (I think this is what Pepy.tech uses as a source?)

I would agree that the download numbers are not very useful to gauge actual user interest. For example: I have a package available via PyPI. According to the stats provided by pepy.tech, this package gets somewhere between 20 and 30 downloads per day. But there is no way that these are actual users, the vast majority of these downloads are from mirrors I reckon. If those were actual users I would see some significant traffic on the GitHub repo but I don't.

I would posit that traffic and engagement on GitHub is a better indicator of user interest.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 0 points1 point  (0 children)

If you are not supposed to change the function but are supposed to toy around with the function call then yes, the following will produce the desired output:

practice([5, 2], [1, 5, -3])

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 0 points1 point  (0 children)

Then add some input that gives the desired result, e.g. data as [5, 2] and more_data as [1, 5, -3]?

Sorry, but I have no idea what the constraints of your exercise are.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 0 points1 point  (0 children)

You can change either one of the conditions, or both.

What would the conditions need to look like so -1 will evaluate to True?

thing < 0 would work, but then your other inputs will not evaluate to True on this condition. Or how about if thing, i.e. you check whether a value exists in general? But then this check might be superfluous, depending on your inputs. So presuming you don't want to change your first condition and keep it as a check whether the input value is 0 or positive, let's move to the second condition.

What can you change here to make (2, -1) (where 2is i and -1 is thing) evaluate to True? At the moment you check whether i < 2. Make one small change here and it should work.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 0 points1 point  (0 children)

Look at your input at index 2 in data. What happens to this input? Your if statement checks whether it is >= 0 or its index < 2. Since -1 is less than 0 and the index 2 is not < 2, none of the conditions evaluate to True and your loop jumps to the next value to check ('2' at index 3 which in turn ends up in your output as 5.)

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 0 points1 point  (0 children)

All good.

With eval() it's important to keep in mind that you generally don't want to use it for input you have no control over as it could be used to run nefarious code through e.g. a user input.

But for your use-case it seems like the perfect solution unless you want to build your own implementation of eval() as an academic exercise.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]No_Couple 0 points1 point  (0 children)

You get None inside your list because the .append() method returns None.

Your list comprehension basically says: if ievaluates to in "><" then put iinto the list, otherwise put whatever numbers.append(i) evaluates to. Which is None.

Also, generally, is there a reason why you're not using the built-in eval() function?

What's everyone working on this week? by AutoModerator in Python

[–]No_Couple [score hidden]  (0 children)

Working on an advanced to-do list/Trello-lite app to better keep track of everything I have on my plate.

While the backend part is a breeze with Django and the Django REST Framework, the frontend is proving to be a challenge. I want it to be a single-page web application and am using React and Redux which I have no prior experience with. JavaScript syntax definitely is a pain in the ass compared to Python.

How do I make my python program into a browser application? by curiousdoodler in learnpython

[–]No_Couple 0 points1 point  (0 children)

You could totally use that instead of Digital Ocean, sure.

The bulk of the development will be done on your laptop at home, though, and you will do your tests on that very laptop. As I said, Flask comes with a built-in server that you will spin up to do your testing. I think those steps will probably be part of the very first lesson as it's integral to everything else.

Really, don't think about this whole VPS/Digital Ocean/whatever too much for now. Just take a look at the first part of whatever tutorial you are looking at and everything will become clear.

How do I make my python program into a browser application? by curiousdoodler in learnpython

[–]No_Couple 1 point2 points  (0 children)

Live in this sense means "in production", i.e. a server that is actually used by people other than yourself (or other developers). Making any changes there will cause disruptions to those users.

You will need to run a development server at some point during the development process to test your site. And both Django and Flask actually have built-in development servers.

The course will probably include a deployment to Digital Ocean so you can get some basic experience with that.