Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to exit 2 loops
#1
for maybe in answer:
    for correct in sequence:
        if correct = maybe:
            right.append("correct")
            break
        else:
            right.append("incorrect")
            break
what i want to happen is it takes a number from answer then run through the list of sequence and if it finds one that matches it stops the loop and moves onto the next number in the answer list:

at the moment it doesnt work
anyone see why?
thanks for the help
Reply
#2
I guess it's the if that cause a problem

= is assignment
== is comparison
Reply
#3
dont worry i fixed that problem but got a new one now

import random

print("Im thinking of a sequence of numbers that is 5 numbers long between 1 and 10")
print("You have to try and guess them in the right order in 5 guesses ")
print("GOOD LUCK")
new = 0

count = 0
sequence = []
num = []
answer = []
right = []
take = ["Take Your First Guess: ","Take Your Second Guess: ","Take Your Third Guess: ","Take Your Forth Guess: ","Take Your Fifth Guess: ",]
howmanytry=0

#generator random sequence:
while count != 6:
        count = count+1
        if count != 5:
            new = random.randint(1,10)
            sequence.append(new)


            
print(sequence)




for attempt in range(1,6,1):
        
        
#take a guess
        for takeaguess in range(1,6,1):
                guess = input(take[howmanytry])
                guess = int(guess)
                answer.append(guess)
                howmany = 0
                right = []
                maybe = 0



#check

        for maybe in answer:
        
                for correct in sequence:
                
                
                        if correct == maybe:
                        
                                
                                howmany = howmany + 1
                        
                                break
                

                
                        
                        
                
        
        
                        

        print("You got",howmany,"in the right order")

        if howmany == 5:
                print("Congrats You Got It Right!")
                exit()

        howmanytry = howmanytry + 1
        
Im thinking of a sequence of numbers that is 5 numbers long between 1 and 10
You have to try and guess them in the right order in 5 guesses 
GOOD LUCK
[10, 10, 7, 10, 3]
Take Your First Guess: 10
Take Your First Guess: 10
Take Your First Guess: 7
Take Your First Guess: 10
Take Your First Guess: 4
You got 4 in the right order
Take Your Second Guess: 10
Take Your Second Guess: 10
Take Your Second Guess: 7
Take Your Second Guess: 10
Take Your Second Guess: 4
You got 8 in the right order
Take Your Third Guess: 
I get this, and I would like to know why it goes up to eight on the second try and it went up to twelve on the third try.

Pls help

The first python code is my code and the second python code is what i get from the shell

the list printed is the answer
Reply
#4
for maybe in answer:

for correct in sequence:

^ i dont think this is right. youre checking each place against each right answer. i think you want this:

check if digit 0 matches answer 0
check if digit 1 matches answer 1
check if digit 2 matches answer 2
check if digit 3 matches answer 3
check if digit 4 matches answer 4

and i think what youre doing with two loops is this:

check if digit 0 matches answer 0
check if digit 0 matches answer 1
check if digit 0 matches answer 2
check if digit 0 matches answer 3
check if digit 0 matches answer 4

check if digit 1 matches answer 0
check if digit 1 matches answer 1
check if digit 1 matches answer 2...

i think you have one loop too many.

you want a single loop (i think) and a check on both sequence and answer with the same loop index.

something like this?

        for dex in range(5):
            maybe = answer[dex]
            correct = sequence[dex]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python difference between sys.exit and exit() mg24 1 1,823 Nov-12-2022, 01:37 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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