|
I am programming a simple game. At the end of the game, I want to give the user an option to play again or exit the program. My logic is as follows: The while loop enters, and if "n" is input, "restart" is set to "n" and the next pass through the while loop should evaluate to false and run the else block. Somehow, this is not happening. (1) code:
(2) code:
(3) code:
|
# ? Aug 7, 2014 02:01 |
|
|
# ? May 8, 2024 21:31 |
|
dantheman650 posted:I am programming a simple game. At the end of the game, I want to give the user an option to play again or exit the program. My logic is as follows: The while loop enters, and if "n" is input, "restart" is set to "n" and the next pass through the while loop should evaluate to false and run the else block. Somehow, this is not happening. If you want to go outside the scope, you can use the global or nonlocal keywords. edit: note that if you referenced restart inside play_again without reassigning it something, it would inherit from the parent scope.
|
# ? Aug 7, 2014 02:08 |
|
ShadowHawk posted:This is python's scoping rules in one of its more simple forms. restart is a new name for a variable you are creating, private to the play_again function, and it's deleted at the end of the play_again function. I thought this might be the case, and I've tried the same code while setting restart as a global variable. No dice - it runs exactly the same. Maybe I'm doing it incorrectly. Don't I just add code:
Harriet Carker fucked around with this message at 02:14 on Aug 7, 2014 |
# ? Aug 7, 2014 02:12 |
|
dantheman650 posted:I thought this might be the case, and I've tried the same code while setting restart as a global variable. No dice - it runs exactly the same. Maybe I'm doing it incorrectly. Don't I just add code:
|
# ? Aug 7, 2014 02:22 |
|
Thinking it over, perhaps a better name for the global keyword might be inherit, similar to how lambda should be called make_function
|
# ? Aug 7, 2014 02:23 |
|
ShadowHawk posted:Yes, within the scope of the function inheriting the variable: Thank you so much. This works perfectly. I had "global restart" outside of the function.
|
# ? Aug 7, 2014 02:26 |
|
Why was the first suggestion globals and not return values. I'm not in a position to explain the finer details of return values right now but you should look them up. It would allow you to have something similar to this:Python code:
Python code:
Edit: That's with python 2, I guess they changed it in python 3 otherwise you would have run into some problems? Namely entering "y" gives a NameError on python 2. Jewel fucked around with this message at 02:37 on Aug 7, 2014 |
# ? Aug 7, 2014 02:35 |
|
Jewel posted:Why was the first suggestion globals and not return values. I'm not in a position to explain the finer details of return values right now but you should look them up. It would allow you to have something similar to this:
|
# ? Aug 7, 2014 02:43 |
|
ShadowHawk posted:Yes, return values are better and more natural here but from reading his "I know it could be done this other way" part of the post I assumed he knew about them. That may not have been correct in retrospect. I don't quite understand the technicalities, which is why I can't see why this works: Jewel posted:
How does this while loop exit? Does return automatically exit the loop? I suppose it must. Jewel posted:Also you should use raw_input because input is just a wrapper around raw_input that also exec's which is bad: Right, it's been changed for Python 3 (as far as I could discern).
|
# ? Aug 7, 2014 02:52 |
|
dantheman650 posted:I don't quite understand the technicalities, which is why I can't see why this works: Python code:
Incidentally now would also be a good time to mention the continue and break statements, which act a bit like returns but are within for/while loops.
|
# ? Aug 7, 2014 03:03 |
|
Thanks for the quick, clear, and concise response. My code is running perfectly now, and I implemented the suggestion of using return values instead of a global variable. Also, using a simple while loop to check if the user entered the correct input is so much better and easier than all that try/except stuff I was using before.
|
# ? Aug 7, 2014 03:09 |
|
dantheman650 posted:How does this while loop exit? Does return automatically exit the loop? I suppose it must. Yeah, "return", "break" both exit a loop (both "for" loops and "while" loops). "continue" is another keyword that skips the rest of the loop and returns to the top. A proper game engine that draws to a window usually has a "while game_running and not window.is_closed()" with some form of sleep inside the loop so it only runs 60 times per second. That's a super simplified version but the base idea is there.
|
# ? Aug 7, 2014 03:11 |
|
ShadowHawk posted:Just a stab in the dark but do you have a namespace collision by defining your own stop() in the above? Nope, I was testing in clean environments. In the end, it seems to me like subprocesses only play nice with the thread's main event loop. I thought it would be nice to avoid the main event loop and always make new ones, because otherwise I'm getting events bleeding from one test to the next, unfortunately. But other than that, things are working. It'll only be an issue for testing.
|
# ? Aug 7, 2014 03:13 |
|
SurgicalOntologist posted:Nope, I was testing in clean environments. In the end, it seems to me like subprocesses only play nice with the thread's main event loop. I thought it would be nice to avoid the main event loop and always make new ones, because otherwise I'm getting events bleeding from one test to the next, unfortunately. But other than that, things are working. It'll only be an issue for testing. edit: eg: https://docs.python.org/3/library/asyncio-eventloop.html?highlight=stop#asyncio.BaseEventLoop.stop If you're defining a coroutine, and coroutines inherit from BaseEventLoop, and you redefine stop(), bad things will happen when stop is normally called. ShadowHawk fucked around with this message at 03:34 on Aug 7, 2014 |
# ? Aug 7, 2014 03:31 |
|
Sure that's overrideable (everything is), but I'm not subclassing anything, Server has no supers. Plus coroutines are just functions, they don't inherit from BaseEventLoop.
|
# ? Aug 7, 2014 04:07 |
|
I have a pandas DataFrame (roughly 3000 rows like below):code:
In the example, 'choice_txt' is an identifier, not a count above. I would like to count how many identifiers exist for each one, in this case there are identifiers 1-10. In 3000 rows, I have one record for each identifier 1-10. Grouped into a final result like this: code:
code:
But I can't figure out how to evaluate the total_paid col by value and use that into the group by. EDIT: OK I have a solution that works, but it seems like a total hack: Since I have 3 groups that I need to accumulate (low,mid,high), separated my main data frame into 3 separate data frames by the low,mid,high criteria: code:
code:
Is this an acceptable solution or is there a better way to achieve the result?
|
# ? Aug 7, 2014 04:53 |
|
Here: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.cut.htmlPython code:
Python code:
Python code:
SurgicalOntologist fucked around with this message at 05:28 on Aug 7, 2014 |
# ? Aug 7, 2014 05:24 |
|
Well I'm pretty proud of my self spending half a day to fix it myself, but your solution is much nicer than mine. I still hate the Panda docs. It's pretty frustrating knowing exactly when I need, what steps to take and hit a brick wall with syntax and libraries.
|
# ? Aug 7, 2014 05:37 |
|
I agree completely. I feel like I learned Python basics in a day just by reading the docs. Same for many other libraries. Not so for pandas, everything is hidden away, even though I think I've read the docs front to back. Features are not that discoverable, e.g. I'm always confused by what's a method and what's a module-level function.
|
# ? Aug 7, 2014 05:46 |
|
BeefofAges posted:I consider metaclasses to be a pretty esoteric language feature, and what I expect most people to know about them is that you probably shouldn't use them unless you have a really good reason. trick answer: it's pass by value, but all python objects live on the heap and variables only store pointers to them.
|
# ? Aug 7, 2014 12:09 |
|
Let's say I have a list of names, what would be the best way to determine whether these are male or female? The easy but hard way would be to just display each name and select the gender, but I'm wondering if I could somehow find a list or something to compare them to. Any ideas?
|
# ? Aug 7, 2014 15:54 |
|
You're probably gonna have to have user input anyway, given the number of names that are gender-neutral.
|
# ? Aug 7, 2014 15:57 |
|
Asymmetrikon posted:You're probably gonna have to have user input anyway, given the number of names that are gender-neutral. Are there that many? Let's say I don't need to be exact, but like 95% accurate on an idea of how many males/females I have in this list.
|
# ? Aug 7, 2014 16:24 |
|
the posted:Are there that many? Let's say I don't need to be exact, but like 95% accurate on an idea of how many males/females I have in this list. Maybe check what kind of coverage you have from these lists: http://www.census.gov/genealogy/www/data/1990surnames/names_files.html
|
# ? Aug 7, 2014 16:27 |
|
Brilliant, thanks.
|
# ? Aug 7, 2014 16:29 |
|
My entire list of names is 1214 When I ran my first test with this list, I got 940 men and 732 women, or 1672 names, meaning I'm getting duplicates somewhere (i.e both male and female name). I tried running a test to print out the duplicates, but I don't think this is working: code:
edit: I think I figured out the problem. Turns out that people tend to have a lot of common names in both genders. "William" was showing up in the womens list, albeit way down at the bottom. I decided to only look at the top 500 male/female names instead. Hopefully it helps. edit: Yep, ended up getting 865 males and 198 females the fucked around with this message at 17:57 on Aug 7, 2014 |
# ? Aug 7, 2014 17:44 |
|
Use genderize.iocode:
code:
|
# ? Aug 7, 2014 17:58 |
|
the posted:My entire list of names is 1214 Python has a built in "set" type that you can use to find intersections, unions, etc. if you ever need to do this again in the future.
|
# ? Aug 7, 2014 18:57 |
|
My question is this -- is python smart enough with the list.copy() function to only duplicate the memory once the copy is actually modified, or does it do it ahead of time?Python code:
Python code:
It seems to me the most efficient way to do this would be to only copy the list once things start munging it. Python code:
ShadowHawk fucked around with this message at 04:53 on Aug 8, 2014 |
# ? Aug 8, 2014 04:51 |
|
Dunno if this is worth posting here but I'm doing some basic data analysis for a physics lab using numpy and have an array which I'm trying to raise to the fourth power but when I check the resultant array all the entries are -2147483648. What's the deal.
|
# ? Aug 8, 2014 07:53 |
|
Trompe le Monde posted:Dunno if this is worth posting here but I'm doing some basic data analysis for a physics lab using numpy and have an array which I'm trying to raise to the fourth power but when I check the resultant array all the entries are -2147483648. What's the deal. Looks like an integer overflow, it's a common issue when you're solving computational problems that involve large numbers. You can either try converting to int64 (if you need to use integers for whatever reason) or using floats
|
# ? Aug 8, 2014 07:57 |
|
Righto, switched the array to floats I guess? Anyway it works now, thanks a bunch.
|
# ? Aug 8, 2014 08:05 |
|
ShadowHawk posted:My question is this -- is python smart enough with the list.copy() function to only duplicate the memory once the copy is actually modified, or does it do it ahead of time? This is kind of a subtle question. A list is a sequence of references to other objects. list.copy() does a shallow copy of the list, that means that a new list is immediately created but it does not recreate the objects, it just copies the references. This may or may not be smart enough for you.
|
# ? Aug 8, 2014 08:31 |
|
Trompe le Monde posted:Righto, switched the array to floats I guess? Anyway it works now, thanks a bunch. When you're working with numpy you have to be careful because you're trading a bunch of python conveniences for speed. numpy ints and floats have limits and you can run into overflows (like 2147483647 + 1 becoming -2147483648). In contrast, with regular python ints and floats you don't need to worry about these things because they have infinite precision (bignum). http://docs.scipy.org/doc/numpy/user/basics.types.html - I recommend studying those datatypes a bit to avoid some lovely bugs later.
|
# ? Aug 8, 2014 08:54 |
|
Trompe le Monde posted:Righto, switched the array to floats I guess? Anyway it works now, thanks a bunch. It depends on what you need to do. Floating point arithmetic is slightly inaccurate. For instance, you might see something like this: Python code:
|
# ? Aug 8, 2014 09:19 |
|
tef posted:Your question struck me as a bit of an X-Y problem, i.e "I'm using generators awkwardly, how do I mitigate it?". Generators are about streams of values, if you're only wanting the first item, something's a bit up. https://github.com/YokoZar/wine-model
|
# ? Aug 8, 2014 11:47 |
|
I got a question today that has me kinda freaked out. I was asked how you would implement a hash table data structure in Python. Not having to worry about making that kind of stuff is one of the reasons that I use Python. I tried, but couldn't come up with a working hash table. Should I be so freaked out?
|
# ? Aug 9, 2014 06:40 |
|
duck hunt posted:Should I be so freaked out? No, you should be learning how to make a hash table.
|
# ? Aug 9, 2014 06:55 |
|
apologies if this is too broad a question for this thread. I am interested getting data from twitter - specifically a list of all the people following a specific account. My goal is compare two such lists and see who is connected to who - this would require a second set of queries. I know a little bit of python, but mostly from homework-type assignments that don't require getting data. I've found the twitter API website but I could use a bit more hand-holding. In short does anyone know a good guide to scraping data from twitter using python? I thought I would narrow my search to python stuff for now, but feel free to suggest alternatives if you know of them.
|
# ? Aug 9, 2014 15:11 |
|
|
# ? May 8, 2024 21:31 |
|
Nippashish posted:No, you should be learning how to make a hash table. Here's my first try today. code:
A related question: I want to implement the array list data structure without using Python's list type code:
|
# ? Aug 9, 2014 23:47 |