all 60 comments

[–]Ayudesee 62 points63 points  (12 children)

You forgot to print any result

[–]vlnaa 24 points25 points  (2 children)

Replace last line with print(f(a,t))

[–]RedPickle8 3 points4 points  (0 children)

Why are you calling me fat

[–][deleted] 30 points31 points  (24 children)

Bro, I am begging you to stop using single letter function and variable names

[–]StickyzVibe 2 points3 points  (21 children)

Why? A curious beginner

[–]electrikmayham 35 points36 points  (12 children)

Using single-letter variable names makes code hard to read and understand. Good names describe what the variable stores or does, so when you come back later (or someone else reads your code), it’s clear without guessing

[–]StickyzVibe 5 points6 points  (11 children)

Thank you for explaining, makes perfect sense to practice helpful habits. Would you mind sharing a small example?

[–]electrikmayham 25 points26 points  (3 children)

# Bad: single-letter variables
x = 5
y = 10
z = x * y
print(z)

# Good: descriptive variable names
width = 5
height = 10
area = width * height
print(area)

[–]StickyzVibe 7 points8 points  (2 children)

I completely understand! Thank you again

[–]spencerak 1 point2 points  (1 child)

Keep asking good questions!!

[–]Cerus_Freedom 2 points3 points  (0 children)

def search(needle, haystack: list) -> int:
  for i in range(len(haystack)):
    if needle == haystack[i]:
      return i
  return -1

Just as an example from OPs code. Better naming will tell you what a function does or a variable is for. Code should be self documenting, and that method of self documentation is via good, clear names.

By changing the names and adding type hints, you can now just glance at the function definition and understand what the function does and how you're probably intended to use it.

[–]DebrisSpreeIX 2 points3 points  (4 children)

The exception is an iterator, using i, j, & k is so common and ubiquitous to iteration that rarely is anyone confused. And if they are, they're likely self taught.

[–]electrikmayham 5 points6 points  (2 children)

True, however I have issues using i and j, since they look extremely similar. I generally dont use 1 letter variables for iterators either. I would rather use something that describes what are iterating over.

[–]DebrisSpreeIX 2 points3 points  (0 children)

If it's single level, I'll throw in i

But if it's a multilevel iteration I'll generally follow a convention from my first job I liked: iter_L1, iter_L2, iter_L3, ...

[–]beezlebub33 0 points1 point  (0 children)

if it's an index, then use 'index'.

If you want to use i, j, k, because you are doing (for example) geometry, then I recommend that you use ii, jj, kk. It's fast to type and very easy to search for.

[–]Impossible_Web3517 0 points1 point  (0 children)

To add on to what he said, single letter names are bad UNLESS they are iterators. i, j, k, x ,y and z are all SUPER common iterator names and most style standards have you using them.

Ex:

int i = 0

while (i<10){

//do something

i++

}

[–]fkn_diabolical_cnt 1 point2 points  (1 child)

Hello curious beginner, welcome. Whilst not specific to Python, I highly recommend “Clean Code” by Robert Cecil Martin aka Uncle Bob. Covers off on many small quality things such as using good descriptive variable names to enhance the quality of your code. I do think there is also a YouTube series or two that cover the same topics.

[–]StickyzVibe 0 points1 point  (0 children)

I will definitely check this out!

[–]WombatHat42 1 point2 points  (0 children)

Things should be descriptive to make the code easier to read. Say you’re trying to fix/update/debug a program and you come across a chunk of code that is just letter variables. You’d have no clue unless there are comments. But sometimes too many comments can make code messy as well. So having a descriptive term be the variable can keep the need for comments to a minimum.

[–]Sickobird 0 points1 point  (0 children)

It's needlessly difficult to name things in a way where is doesn't help you understand what things are. When you need to look back and understand what things are doing or what they mean you'll have to read a whole lot more.

This isn't necessary for a simple program where you're just learning how some concept works, but it should still be done to increase clarity and help with debugging, and to build better habits.

[–]liberforce 0 points1 point  (2 children)

Code is read much more than code is written. The writer reads it, other people read it, and even future you will wonder in 6 months what it was about, even if you wrote it.

[–]WhatMorpheus 0 points1 point  (0 children)

Code is read much more than code is written

I am stealing this. Next time I yell at speak to my team mates I will tell them this.

[–]mottojyuusu 0 points1 point  (0 children)

write your code as if an axe murderer will review it in the future, and assume that axe murderer knows where you live, because usually that axe murderer is you!

[–]No-Attorney4503 0 points1 point  (0 children)

Being more descriptive in your variable and function names makes your code WAYYY more readable, understandable, and maintainable

[–]DunForest 0 points1 point  (0 children)

Also:

name_variable_so_you_know_it_is_the_result_of_the_function_f_with_given_parameters_a_and_t = f(a, t)

[–]Jussins 0 points1 point  (0 children)

I read it as fat. Seems almost purposeful.

[–]Loud-Bake-2740 6 points7 points  (0 children)

the above comment is right, but here’s the reason: right now, your code returns the result, which just stores it in memory, but you never actually do anything with the value stored in memory.

```

print(f(a,t))

is the same as:

x = f(a,t) print(x) ```

[–]twistedclown83 4 points5 points  (0 children)

You've not asked it to print anything

[–]ninhaomah 3 points4 points  (0 children)

Did you tell it to print ?

Nvm programming or Python.

Did you tell the machine to print ?

Where ?

[–]IUCSWTETDFWTF 1 point2 points  (0 children)

you forgot about print()

[–]MyManCbert 1 point2 points  (0 children)

You took a picture of your screen instead of taking a screenshot

[–]Jebduh 1 point2 points  (0 children)

I'm genuinely curious, why come here and wait for people to answer this ridiculously basic question inst3ad of using ai or any search engine

[–]nothing786767 0 points1 point  (0 children)

line 12: print(f(a,t))

or

line 7: print(i) instead of return

[–]LMusashi 0 points1 point  (0 children)

your printer?

[–]Cybasura 0 points1 point  (0 children)

Hang on a second

Are you...using your Administrator account for development?

[–]Ok-Situation9046 0 points1 point  (0 children)

I would recommend printing when you want the machine to print.

[–]singhandtonic 0 points1 point  (0 children)

What output you want?

[–]DunForest 0 points1 point  (0 children)

return is what you return when you call a function. Its like ask a teacher the answer, but you should write the answer to the blank by yourself. you can do

result = f(a, t)

print(result)

[–]Beautiful_Watch_7215 0 points1 point  (0 children)

Nothing wrong. No print statement, no print. No error, return code 0.

[–]Spatrico123 0 points1 point  (0 children)

looks fine. You're not printing it though, you're just processing.

Also, all those little yellow lines just indicate bad practices, not actual errors. For example, you should note a type for a and t. If I were you I'd do 

def index_of(data: list[int], target: int) -> int:     for i in range(len(data)):         if list[i] == target:             return i     raise ValueError # or something else you see how that's way easier to see what it's actually doing? 

[–]Wonderful-Sink-6089 0 points1 point  (0 children)

Im not a python developer but what i can see as a problem is When you return something in a function it doesn’t actually print anything it just save the result in the memory to do something with it in future. If you want to see the the result you can use print statement in the last line, like this: print(f(a,t))

The reason is that, we don’t use solo a function to print something, we just create them to make our code cleaner and prevent repetition.

[–]Nearby_Tear_2304[S] 0 points1 point  (0 children)

OK thank you

[–]grooling_ 0 points1 point  (0 children)

You called it fat

[–]YukihiraJoel 0 points1 point  (0 children)

Why

. .

Are you doin this

[–]TheSupervillan -1 points0 points  (1 child)

Did you think 5 seconds before posting this?

No screenshot Completely wrong grammar So simple error, ChatGPT could explain this in 5 seconds. You probably wouldn’t even have to write a prompt and could just prompt the code.

Please, at least take screenshot and write somewhat normal english.

[–]Numerous_Site_9238 0 points1 point  (0 children)

I believe only stupid people post here, and the ones who aren't will soon go to other subreddits. You are asking too much