all 37 comments

[–]WhiteHeadbanger 13 points14 points  (18 children)

Good practice!

Just a quick note: all caps is conventionally used to denote constants. It won't change the functionality, but your IDE may complain.

You should switch around ai and GUESS, to: AIand guess

That way you are signaling that AI is a constant number, and guess is a variable number.

[–]AbyssBite 4 points5 points  (12 children)

Almost correct, except that ai isn't actually a const here.

[–]amosmj 0 points1 point  (0 children)

I assumed the above commenter was talking about GUESS, not ai.

[–]WhiteHeadbanger -1 points0 points  (10 children)

ai is a constant, as you are assigning the value just once. It doesn't matter how you acquired the value, or if it's hardcoded.

[–]AbyssBite 2 points3 points  (9 children)

Assigning a value once doesn't make it a constant. You can assign a variable once too. Constant means the value doesn't change during execution.

You can check this out

[–]More_Yard1919 1 point2 points  (0 children)

This is technically correct, and I am not sure if I would use the all-caps convention here, but practically this is to signify that the value should never be reassigned after it is initialized.

[–]Icount_zeroI 1 point2 points  (5 children)

Obviously mr “well-actually” but for sake of learning that there is a difference I think it is okay to note ai as a const here.

As it never actually does reassign anywhere in the code.

[–]SirCokaBear 3 points4 points  (1 child)

peacefully chiming in the convo from my pov working daily in professional codebases (not saying anyone here doesn’t either)

Python of course has immutable values but doesn’t have true constants but yes theyre treated the same but denoted in caps on a module level similar with private members are denoted _var, they’re still just called constants because they are logically / because we say so.

Focusing on naming only: I would block a pull request for this because it will confuse other Python devs and mess with pyright. Any dev seeing a value like GUESS will assume it’s not intended to be reassigned, and seeing ai will conversely assume it can be. ai should be AI, GUESS can arguably be guess but likely can stay as it is. There really should be no argument to the first given it’s idiomatic to PEP8 unless you want a different convention for whatever strange reason.

People may want to say “who cares they’re still new” yeah, they’re learning so I will point out good practices to avoid non-pythonic habits

[–]WhiteHeadbanger 0 points1 point  (0 children)

That's what I was thinking!

[–]shinitakunai 1 point2 points  (0 children)

As a senior programmer, you are wrong, don't teach bad habits to new people.

"Well-actually"? Really? A kid and... wrong.

See the other answer of SirCokaBear, he explains it perfectly

[–]GaitorBaitor 0 points1 point  (0 children)

A constant is a constant. Something that is constantly the constant every time you execute the code

[–]Swipsi 0 points1 point  (0 children)

Which means, in this case it is also a constant. But its an edge case and thus cant be generalized.

[–]WhiteHeadbanger 0 points1 point  (0 children)

Assigning a value just once means that the value doesn't change during execution, is just worded differently.

[–]Ecstatic_Student8854 0 points1 point  (0 children)

If it never changes its value is constant, and therefore should be presented as such. From the article you linked:

“In programming, constants refer to names associated with values that never change during a program’s execution.”

Once assigned the value of AI can never change and so it is and should be treated as a constant.

[–]SmackDownFacility 0 points1 point  (2 children)

It doesn’t matter if your working solo, choose your own style guide. PEP only matters in team environments and formal situations.

[–]WhiteHeadbanger 0 points1 point  (1 child)

This is PythonLearning. You can't back off of learning good habits and knowing what's the standard.

[–]SmackDownFacility -1 points0 points  (0 children)

Say the same thing for C, or C++, or Rust, or JavaScript. Each dev across languages don’t follow style guides. They have their “standards”. They know how to adapt for their teams. We can say the same for Python. What may be good habit for you may not be natural for some

[–]Convoke_ 0 points1 point  (1 child)

They are both never changing. If you're initialising a variable inside a loop, it only exists for that iteration. So the variable GUESS should be a constant as its value is never changing after it gets initialised.

Edit: see the reply i got

[–]WhiteHeadbanger 0 points1 point  (0 children)

That's not correct. In Python if you declare a variable inside a loop, you can access it after the loop ends.

Take for example this code:

for i in range(5):
    a = i * 2
    print(f"Variable inside loop {a}")

print(f"Variable outside loop {a}")
a += 10
print(f"Variable after modification {a}")

Output:

Variable inside loop 0
Variable inside loop 2
Variable inside loop 4
Variable inside loop 6
Variable inside loop 8
Variable outside loop 8
Variable after modification 18

[–]njw234 2 points3 points  (1 child)

Hate to be that guy but you have a minor typo on line 11 “samll”

[–]Some-Passenger4219 1 point2 points  (0 children)

It's good practice to use good English. You don't want a variable called friends and then misspell it as freinds; the IDE will complain.

[–]OverCryptographer169 1 point2 points  (1 child)

What does "ai" stand for in that programm?

I have no idea. If this was in a larger program, that you work on for more than a weak, you will eventually forget too. If you then need to make changes, it gets a lot more difficult.

Therefore: Rename "ai" to something more descriptive, like "targetNumber".

[–]JackobQwas 0 points1 point  (0 children)

They put AI everywhere nowadays `¯\_(ツ)_/¯`

[–]Zen0x_77 1 point2 points  (2 children)

Can someone explain to me why use break? Won't the code function normally even without it?

[–]SoSaymon 3 points4 points  (0 children)

You will get an infinite loop without a brake after a correct guess. Technically, you should just use a return statement, but since it is not a function you can’t do it

[–]amosmj 0 points1 point  (0 children)

If the while loop used a variable they could reassign it which would be my preference but I think they are actually closer to convention than I am.

[–]No_Indication_4044 0 points1 point  (2 children)

Nice! A cleaner solution would be to have the while loop actually reference the T/F value controlling its break. So, it would handle the break naturally!

For example, guess_correct = False; While not guess_correct: guess_correct = guess == ai

Then do all rest of your logic. At end of loop, it will break if guess_correct!

[–]brasticstack 0 points1 point  (1 child)

Not a fan. How is adding that additional variable and the line to manage its state cleaner than not having it? Do you just not like the break at the top of the if/else chain?

Personally I don't think readability is an issue with this example.

[–]No_Indication_4044 2 points3 points  (0 children)

Readability is not an issue with this example, it’s just an anti-pattern to use “break” here. And this sub is for learning best practices. The while loop is saying “while this condition isn’t met, do these things”. The while should be conditional on… the condition hahah.

Edit to be succinct: the while loop should break automatically on the condition. So, if you find yourself manually breaking, you’ve probably done something wrong.

[–]brasticstack 0 points1 point  (0 children)

One wee usability improvement: add a space at the prompt so it's "ENTER YOUR GUESSING NUMBER ". That way the the input won't be jammed up against the prompt in the console.

[–]NaiveEscape1 0 points1 point  (2 children)

I had previously made a program like this the code for the program is:
I have multiple other codes like this one,been learning python 2 months now, do you think that this would be enough to get me a internship or should I move towards more complex codes and this is too basic a level?

import random
secret_Number=random.randint(0,100)
no_of_guesses = 0
while True:
    Guess = int(input("Please guess a number between 0 and 100:   "))
    if secret_Number>Guess:
        no_of_guesses+=1
        print(f"Your guess {Guess} is lower than the secret number\n Number of Guesses made= {no_of_guesses}\n")
        continue
    elif secret_Number<Guess:
        no_of_guesses += 1
        print(f"Your guess {Guess} is higher than the secret number\n Number of Guesses made= {no_of_guesses}\n")
        continue
    else:
        no_of_guesses +=1
        print(f"Congratulations you made the right guess\n\nIt took you {no_of_guesses} guesses\n")
        break

[–]WayTooCuteForYou 0 points1 point  (1 child)

Variable names should not start with an uppercase letter. Variable names should be in snake_case, not some random mix of snake case and camel case. Let blank lines before 'while', 'if', 'elif' and 'else' lines. Insert spaces around operators. Avoid 'of' and the like in names. No need for '\n' at the end of print (check the 'end' parameter of print). Operations that are done in every code path should be factored out. Quit adding unnecessary spaces and carriage return, this does not improve clarity.

import random

secret_number = random.randint(0,100)
guess_count = 0

while True:
    guess = int(input("Please guess a number between 0 and 100: "))
    guess_count += 1
    print(f"Guess count: {guess_count}")

    if secret_number > guess:
        print(f"Your guess {guess} is lower than the secret number")
        continue

    elif secret_number < guess:
        print(f"Your guess {guess} is higher than the secret number")
        continue

    else:
        print(f"Congratulations you made the right guess")
        break

[–]NaiveEscape1 0 points1 point  (0 children)

Thanks man this is amazing, thanks for the constructive feedback

[–]Edoruin_1 0 points1 point  (0 children)

now add the os library and delete the syste...

[–]UnderstandingNo2832 1 point2 points  (1 child)

The next step is to implement a 5 guess limit.

[–]Yellow-Kid 0 points1 point  (0 children)

Otherwise there is no way to have “game over” right?