# LOOPS_Your own while / else

#1

MY CODE RUNS
Still i wanna be sure it it is OK.
because it is a big code.
ATTACHED PIC FOR INDENTATION.

CODE:

``````import random
from random import randint

# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)

guesses_left = 3
count = 0
while guesses_left>0:
while count < 3:
num = random.randint(1, 6)
print num
if num == guess:
print "you win!"
break
count += 1
guesses_left-=1
else:
print "You lose!"``````

#2

you generate a new random number each iteration of the loop? This makes it a really difficult guess game

#3

Also,
it is always printing "you lose" at the end of the three iteration.EVEN AFTER I HAVE WON

#4

why do you have two loops? One loop should be enough, you need to prompt the user 3 times

#5

outer loop is for counting no of guesses.
inner loop is for generating 3 numbers.

Out of every group of nos (group of 3 in outr case) , we have to match if our guessed no is present in that triplet .

#6

your program is different from the exercise, okay

now, we need to modifications to secure that you loose isn't printed when guessing correctly

in which case we get something like:

``````import random
from random import randint

# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)

won = False
guesses_left = 3
count = 0
while guesses_left>0:
while count < 3:
num = random.randint(1, 6)
print num
if num == guess:
print "you win!"
won = True
break
count += 1
if won:
break
guesses_left-=1
else:
print "You lose!"``````

now the outer loop will also break if the user gets it right, we use a boolean value to verify this

#7

A post was split to a new topic: Your own while else

#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.