In the for-loops x doesn´t know anything :-)
x is used as an index to address single items in the variable code which is a string.
If you enter 4 digits the input() functions returns a string e.g. '1234'.
Each character in the string can be accessed by using brackets e.g. code[0] is '1', code[1] is '2' etc.
range(0,4) is an iterator function that assigns the variable x in the for-loop the values 0, 1, 2, 3
after another. If the start is 0, that value can be omitted, so range(4) does the same.
But!
In Python you can iterate directly over the contents of a string.
I refactored your code a little bit to give you some hints how things can be done.
x is used as an index to address single items in the variable code which is a string.
If you enter 4 digits the input() functions returns a string e.g. '1234'.
Each character in the string can be accessed by using brackets e.g. code[0] is '1', code[1] is '2' etc.
range(0,4) is an iterator function that assigns the variable x in the for-loop the values 0, 1, 2, 3
after another. If the start is 0, that value can be omitted, so range(4) does the same.
But!
In Python you can iterate directly over the contents of a string.
for character in code: if int(character) < 1 or int(character) > 6: fout = 1is the pythonic way to do that verification.
I refactored your code a little bit to give you some hints how things can be done.
import random def main(): while True: # invoer code door de speler code = input("geef je code van 4 cijfers op (stop met 9999):") if code == '9999': break # controleer op cijfers only if code.isdigit(): # controleren op 4 cijfers if len(code) == 4: # controleer dat de cijfers < 1 of > 6 fout = False for cijfer in code: fout = cijfer in '0789' or fout if not fout: # controleer of all getallen verschillen zijn gelijk = False for x in range(4): for y in range(x+1, 4): gelijk = code[x] == code[y] or gelijk if not gelijk: print("your code meets all conditions") break else: print("twee of meer cijfers zijn hetzelfde") else: print("cijfers mogen niet lager dan 1 of hoger dan 6 zijn") else: print("er zijn te weinig of te veel cijfers") else: print("er staan letters in") print("all is well") if __name__ == '__main__': main()