Python Forum
codebreaker exercise visualsteps
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
codebreaker exercise visualsteps
#1
HI,

Just starting out with python. Throught my book, a real basic book from visual steps I came to make below code. I think I understand all the working but one thing potentially puzzles me.

I use this: for x in range(0,4): a few times. Now I understand what the loop does and that is is nested further down the code but, how does the interpreter understand that x comes from the number range which was initially put in under the variable "code". Even thinking about the indentation does not explain it to me fully.

Anybody who can explain in baby logic :-) how python interpreter knows that x is processing the numbers given in the variable code (assuming I have that right).

Any help appreciated.

Thanks,
Ivo
import random
import sys
while True:
    # invoer code door de speler
    code = input("geef je code van 4 cijfers op (stop met 9999):") 
    # controleer op cijfers only
    if code.isdigit():
        #stop met het spel
        if int(code) == 9999:
            sys.exit()
        # controleren op 4 cijfers
        if len(code) == 4:
            
            # controleer dat de cijfers < 1 of > 6
            fout=0
            for x in range(0,4):
                if int(code[x]) < 1 or int(code[x]) > 6:
                    fout=1
            if fout == 0:
                
                # controleer of all getallen verschillen zijn
                gelijk=0
                for x in range(0,4):
                    for y in range(0,4):
                        if x != y and code[x] == code[y]:
                            gelijk=1
                if gelijk == 0:
                    
                    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")
Reply


Messages In This Thread
codebreaker exercise visualsteps - by ivobrugman - Aug-10-2019, 08:12 PM
RE: codebreaker exercise visualsteps - by perfringo - Aug-11-2019, 08:35 AM
RE: codebreaker exercise visualsteps - by ThomasL - Aug-11-2019, 09:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  CODEBREAKER game rob101 0 904 Nov-07-2023, 07:08 AM
Last Post: rob101

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020