# Rate my code, I'm a beginner

``````#NUMBER GUESSING GAME.PY

import random
lower_bound=1
upper_bound=500
max_attempts=10
secret=random.randrange(lower_bound, upper_bound)
guess=int(input(f"Guess a number between 1 and 500, you have ten tries"))
while guess != secret:
if (guess > secret) and (max_attempts>0):
guess=int(input(f"Too big"))
max_attempts = max_attempts-1
if (guess < secret) and (max_attempts>0):
guess=int(input(f"Too small"))
max_attempts = max_attempts-1
if max_attempts < 1:
if guess == secret:
print("You got it, you had this many tries left):")
print(max_attempts)
``````

It doesnâ€™t need to be an f-string here:

`guess=int(input(f"Guess a number between 1 and 500, you have ten tries"))`

It gives the user 11 attempts.

If the user fails to guess the number and `attempts` drops to 0, itâ€™ll loop forever, printing â€śToo bad, try againâ€ť.

It should loop until the user guesses correctly or the maximum number of attempts has been reached, and then print the final result.

Oh, and the recommendation is to indent by 4 spaces.

I like the indenting after `if` to look like:

``````while guess != secret:
if guess > secret and max_attempts > 0:
guess=int(input(f"Too big"))
max_attempts = max_attempts-1
``````

You should always group common conditions together and add spaces for clarity.

``````import random

lower_bound = 1
upper_bound = 500
max_attempts = 10

secret = random.randrange(lower_bound, upper_bound)
guess = int(input(f"Guess a number between 1 and 500. You have ten tries: "))

while guess != secret and max_attempts > 0:
if guess > secret:
print("Too big")
else:
print("Too small")

max_attempts -= 1
if max_attempts > 0:
guess = int(input(f"You have {max_attempts} tries left. Guess again: "))

if guess == secret:
print("You got it! You had this many tries left: ", max_attempts)
else:
``````
2 Likes

You mean that the â€śifâ€ť is at a half indentation level? I wouldnâ€™t recommend that; some people might do it for a match/case statement (since the match and case are intrinsically linked), but a while/if pair doesnâ€™t really justify half-an-indent.

BTW, four spaces is only one common convention; use of a single tab as indentation is also extremely common. But I agree with MRAB that a two-space indentation looks â€śpacked tightâ€ť. Itâ€™s a convention that works in JavaScript where programs can often be incredibly deeply nested, but it isnâ€™t necessary in Python.

Yes, 3 spaces (I hate `TAB`s). With many `if ...`, itâ€™s IMHO prettier that all actions line up:

``````a, b = whatever
if a == 0:
do_something()
if a > 0 and b == 0:
do_something_else()
if a <= 0 and b > 0:
and_now_for_something_completely_different()
``````

They would do that if you indent all the way though. With four spaces or a single tab, the actions would still be aligned, a second indentation level in from where the `if` statements are.

I assume that means you would indent `while` and `for` loops similarly:

``````while a == 0:
do_something()

for i in range(10):
do_something()
``````

Iâ€™m not sure how you would handle `if-elif` statements:

``````if a == 0:
do_something()
elif a == 1:
do_something_else()
``````

or

``````if a == 0:
do_something()
elif a == 1:
do_soemthing
``````

or

``````if a == 0:
do_something()
elif a == 1:
do_something_else()
``````

Thereâ€™s nothing wrong, per se, with this, but since most editors can be configured to indent automatically following a line ending with `:`, itâ€™s simpler to stick with a uniform amount of indentation per level, regardless of the type of statement that induces the indentation.

If you ever work on code with other people, I think you may find it hard to convince them to use your indentation style, though. Uniform 4-space indentation is nearly universal (and most of the outliers are still uniform, but using more or fewer spaces). The only common exceptions relate to the indentation of continued lines; see PEP 8â€™s section on indentation for examples.

2 Likes

What I think the OP means is that a three-space indent for an `if` block aligns the contents of the block with the condition for the block (two spaces for the keyword `if` itself, and one for the space after that).

In the long term itâ€™s not just about what the author prefers the code to look like, because other people will want to read it (hopefully!). Style conventions are nice for this reasonâ€“when code is styled consistently, people know where to look to figure out how the code works.

I would recommend picking a formatter (`black` and `ruff format` are good choices) and letting it make those decisions. After a little bit of time the style will feel natural.

1 Like

Ohhhhhh. I honestly didnâ€™t even spot that. Was not obvious.

I use 2 spaces in my personal code. In shared code, whatever the
convention there is (usually 4 spaces).

I used to use 3 spaces in Pascal

1 Like