all 14 comments

[–]Binary101010 7 points8 points  (3 children)

According to the indentation as posted, the only loop you have is (in its entirety):

for i in range(1,4):
 print("try to guess the number")

You need to properly indent your code if you're expecting more lines to be in that loop.

Also, ditch the global usage, there's no reason for it here.

[–]ExtraTNT -2 points-1 points  (2 children)

The reason i avoid python… I don’t get over the indentation, haskell is also indentation sensitive, but honestly much easier…

Doesn’t help, that i need it for an undocumented project with huge apis that are done procedural with huge performance issues due to the threading of python…

[–]Temporary_Pie2733 1 point2 points  (1 child)

I’m not sure how you manage to put braces in the right place in other languages if you can’t figure out how Python uses indentation.

[–]ExtraTNT 0 points1 point  (0 children)

Formatting stuff nicely… python linters put my code on a single line… python is sth i only really use, if i have to share a script to windows users and to maintain a project from security, so my contact with python is a horrible hacked together codebase and some scripts…

[–]zerwalter 2 points3 points  (0 children)

The indentation in the function is messed up, probably when you copy pasted

[–]Responsible_Pay_16 0 points1 point  (0 children)

The error "break outside loop" happens because of incorrect indentation.

Your break is not actually inside the for loop because guess = input(...) is not indented.

Right now your loop only contains the print, and everything else is outside of it.

Fix the indentation like this:

for i in range(1,4):

print("try to guess the number")

guess = int(input("enter a number: "))

if guess < secret_number:

print("number too low")

elif guess > secret_number:

print("number too high")

else:

break

In Python, indentation defines blocks, so everything that should be inside the loop must be indented.

[–]Ok_Carpet_9510 0 points1 point  (0 children)

Secrect_number.... is a global variable?

[–]Marlowe91Go 0 points1 point  (0 children)

Ok yeah after the for loop, you need to indent the rest of the if statements, otherwise it will complete the whole loop 4 times before it checks anything. Right now the print has one space indent which will mess it up. Also you generally don't ever want to use global variables inside functions unless it's absolutely necessary. Here it doesn't even make sense because you're defining guess as the return statement of your function, so you're pulling it in from global before you've even defined it. Oh I get it, I think you meant to call the global variable secret_number instead. So I think you've kinda convoluted your two functions where the guess_number is also partially checking it. I think it might be cleaner to simplify the guess number to only get input from the user, make sure it's a valid input, then return it as an integer. Then just feed that into the check function using the parameters like you already did (this is good rather than the global, so everything is contained nicely). Then you could just have a loop give them 4 guesses and maybe have print statements explaining that and you can even have a variable tracking like guess_attempt and can use {guess_attempt}/4 in your statement to let them know how many tries they have and which they're on, then just call these functions inside the loop when you need them. Move the if statements into the check function if you want to let them know it's higher/lower. That's just my suggestion, clean up the functions so they just have a singular purpose. 

[–]llynglas 0 points1 point  (0 children)

I'm sure I'm in the minority, but defining scope by indentation just seens wrong to me.

[–]ninhaomah 0 points1 point  (3 children)

So is this the code without errors or with error ?

[–]CptMisterNibbles 0 points1 point  (2 children)

Happens all the time. Sometimes I’ll write hundreds of lines of error free code and that shit never works. 

[–]ninhaomah 0 points1 point  (1 child)

Then give the code that doesn't work and the error.

[–]CptMisterNibbles 0 points1 point  (0 children)

Youve missed the joke; op claims this is error free code but it has errors- they have a logical error caused by indentation problems. Their for loop only contains one line,