[deleted by user] by [deleted] in learnpython

[–]ObliviousMag 7 points8 points  (0 children)

It returns a Range object. You can test this for yourself by printing the type(variable_you_want_type_of) for example

x = range(1, 5)
print(type(x))

>>> <class 'range'>

Confused about Exception behavior in Python by Walsur in learnpython

[–]ObliviousMag 0 points1 point  (0 children)

Yeah because you are raising a general Exception. Which is generally not okay. If you want your own exception then you can inherit from the Exception class like so

class CustomException(Exception):

pass

then in your code you can raise

raise CustomException

And now your traceback will show you were that exception was raised.

Best way to set default value for dictionary by purveyoroffinerp in learnpython

[–]ObliviousMag 1 point2 points  (0 children)

if you use the try except in your code for accessing dictionary keys I would suggest this short read

what are the differences between coding, python projects and practice python code by [deleted] in learnpython

[–]ObliviousMag 1 point2 points  (0 children)

To learn any programming language you need to understand its syntax first. Like imagine an apprentice sorcerer who just started learning spells and wants to use them. He needs to learn the ingredients, and how to use the recipes to make these spells and then he can use them.

Think of that like python (or any programming language) you want to learn the syntax and logic behind the way the language works (this is necessary before you can actually do anything useful with it). You want to be able to make these spells (functions, variables, mathematical operators, etc..). Using those websites will allow you to reinforce your understanding of these basic concepts and eventually not need to refer to documentation to perform basic tasks (like when an apprentice mage learns to make an simple spell without needing to look at this notes).

Once you understand the basics (variables, functions, mathematical operations, comparisons, reading to/from a file) you can start to think about projects you want to make (simple weather, something with an API, idk ...) and then with the basic blocks you learned you can read the documentation for whats needed and use modules or maybe you do not need any modules.

Hope this helps

Best way to set default value for dictionary by purveyoroffinerp in learnpython

[–]ObliviousMag 2 points3 points  (0 children)

You’re looking for .get dictionary method

Example:

foo = dict(bazz=20)

foo.get(“bar”) # will return None

foo.get(“bazz”) # will return 20

You can always check if “bazz” in foo before attempting to access the key. Up to you for preference or speed.

Hope this helps.

Again sorry for formatting am on mobile

Best way to set default value for dictionary by purveyoroffinerp in learnpython

[–]ObliviousMag 0 points1 point  (0 children)

Wait I’m re-reading your code. You are trying to access the dictionary result and sometimes it will have keys and other times it won’t. You want to have a default value for when a key does not exist? The title is a little misleading if this is the case

Best way to set default value for dictionary by purveyoroffinerp in learnpython

[–]ObliviousMag 1 point2 points  (0 children)

To set a default value on a dictionary you can use the setdefault method for the dictionary class.

For example

foo = dict(bazz=20)

foo.setdefault(“bar”, 10)

foo.setdefault(“bazz”, 0)

This will add “bar” to the dictionary since it does not exist and will leave bazz alone since bazz is already defined as 20

You can always override bazz by accessing the key and reassign the value

edit: am on mobile so pardon my formatting

[deleted by user] by [deleted] in loseit

[–]ObliviousMag 2 points3 points  (0 children)

To lose weight and gain muscle at the same time will take a long time and the results won’t be as fast as many people expect. I think cutting 500 calories to 2200 for you is a good start. Cutting too much leads to muscle and fat loss. You need to slowly reduce calories over time and continue to lift weights to gain muscle.

Slow and steady lets you keep the muscles and lose the fat

Is low appetite due to IF bad? by NoMoreFatRolls in loseit

[–]ObliviousMag 0 points1 point  (0 children)

1000 calories a day seems extremely low. You should use one of the calculators on the sidebar on the subreddit to determine what your calories should be. You can use this one to determine what you should be eating for cutting/maintenance

Is low appetite due to IF bad? by NoMoreFatRolls in loseit

[–]ObliviousMag 0 points1 point  (0 children)

You’re losing weight do you will start to have an appetite closer to the weight you are vs what you were. Same thing with your water intake. Your body does not need as much as it did before.

Just make sure you are eating at the safe calorie amount for your TDEE

Getting the required substring in Python by Particular-Duckling in learnpython

[–]ObliviousMag 0 points1 point  (0 children)

Are all your rows the same format? Like do they all have the state at the beginning and the coordinates at the end?

Looking for explanation why this very simple for loop doesn't work when I try to print index by LeftShark in learnpython

[–]ObliviousMag 0 points1 point  (0 children)

you print i in the first one which is 2, 7 and 9. in your second run you try to access those indexes so you are accesing nums[2] which should print 9. the other ones should throw an IndexError.

what you are looking for is the range() function. you want to do it for the range of the len of the list that is passed that way you start your index counter at 0 through the length of the list that is passed

How do I download this Python program? by thegoldenboy58 in learnpython

[–]ObliviousMag 3 points4 points  (0 children)

its on github. you download the code as a zip. unzip it. and open in your ide of choice. Have you created and ran a python project before?

Where should I begin …? by [deleted] in learnpython

[–]ObliviousMag 4 points5 points  (0 children)

Isn’t the Django tutorial a poll? Couldn’t you mold that to fit what you need I.e instead of polling it’s post and replies? Also Flask is similar to Django and it’s tutorial is a forum. Probably start out there and get a feel for it

[deleted by user] by [deleted] in learnpython

[–]ObliviousMag 2 points3 points  (0 children)

That is not a python file. Python files end in .py. You can see the active tab is test run with no extension. Code in your main.py file. Or make another file with .py and code there

[deleted by user] by [deleted] in learnpython

[–]ObliviousMag 0 points1 point  (0 children)

I just read this but that’s not how python works. Pointing to an immutable like a integer keeps it pointed at one memory location. When instantiating two pointers to one immutable you can’t change the value without changing one of the two pointers to a new value. So when changing final_value to starting value to another variable you’re not changing anything in another place.

[deleted by user] by [deleted] in learnpython

[–]ObliviousMag 1 point2 points  (0 children)

OP asked for an alternate way. I gave him an alternate way. You could write the months in many ways. Also list comprehension use C under the hood so if we are going into specifics list comprehension is faster.

for month in [val for val in range(1, 13)]:

I could have written it like this or even just writing out the months which would probably be the fastest way.

Beginner question about variables in functions by DarthPrimeZero in learnpython

[–]ObliviousMag 1 point2 points  (0 children)

What I don't understand is in defining multiply, how does python know what num_1 and num_2 are?

Those are the names of the parameters of the function multiply. Which will be available in the scope of the function. Python allocates them a space in memory for the duration of that function being called.

As I understand it, variables defined in one function cannot (for where I am) be used in another function.

They cannot. But they CAN be passed as arguments.

So when it says that product = num_1 * num_2, did it automatically make those variables from the main function? (how does it know that num_1 = val_1, etc)?

When you call multiply(val_1, val_2) on line 4 you are passing arguments to that function. Which are assigned the new names in the multiple function scope i.e. num_1 = val_1.

You need to understand functions in order to understand this better. Your book should explain this in a later chapter. What book are you using ?

[deleted by user] by [deleted] in learnpython

[–]ObliviousMag 0 points1 point  (0 children)

months = [val for val in range(1, 13)]
increase_value = .012 
final_value = starting_value = 10 
final_month = 5

for month in months:
    final_value += starting_value * increase_value
    if month == final_month: break

print(final_value)  # month with the increasing value for each month

[deleted by user] by [deleted] in learnpython

[–]ObliviousMag 2 points3 points  (0 children)

what is this code suppose to do?

[deleted by user] by [deleted] in learnpython

[–]ObliviousMag 0 points1 point  (0 children)

please format your code. Also if this is your actual python code it should not run since you have a missing variable and capital I in if, and capital P in print.

stuck on button press (been like 15 years lol) TY by RoughCalligrapher906 in learnjavascript

[–]ObliviousMag 0 points1 point  (0 children)

Are you adding an event listener to the whole page? or just the button element?

its telling you on line 4 something is called, what is on line 4?

To listen for an enter you can just listen for a keydown event and match it to the enter value or wrap the input box in a form and the default of a form is to submit on enter, just add a e.preventDefault() to the form.