## How to loop until a certain condition happens?

This is the place for queries that don't fit in any of the other categories.

### How to loop until a certain condition happens?

Hey im new to python and programming altogether really, and im just looking for a little assistance with a piece of code.

Code: Select all
`score = 0import random   i = random.randint(0,4)if (i == 0):    print("ya got red, ya gone")    print(score)if (i == 1):    print("ya got blue, ur right")    score += 10    print(score)if (i == 2):    print("ya got green, ur right")    score += 15    print(score)if (i == 3):    print("ya got yellow, ur right")    score += 20    print(score)if (i == 1):    print("ya got purple, ur right")    score += 25    print(score)if (i == 4):    print("ya got orange, ur right")    score += 30    print(score)`

its gonna be a game where you get given a random color and get points for whatever the color is; if you get red your turns over and it adds up your score and switches to player 2's turn.

I need a loop so it keeps adding different random numbers until 0 is randomly chosen. example: 2,4,2,1,3,0. the game would end at 0 and would switch players. I'm just not sure on how to have it loop so it continuals until a certain integer is selected.
if anyone can help it'd be great, or if you see any way I can better my code please do comment, any and all help is appreciated
Last edited by Yoriz on Wed Mar 26, 2014 1:11 pm, edited 1 time in total.
Reason: First post lock, Changed Title
billygo

Posts: 4
Joined: Wed Mar 26, 2014 9:50 am

### Re: Need help with easy short code

You will obviously need a loop to make this happen.
Here are two possible ways:
• Using a while loop and a break:
Code: Select all
`while True:    number = random.randint(0, 4)    if number == 0:        break    # do stuff with number`
• Using a for loop and iter():
Code: Select all
`for number in iter(lambda: random.randint(0, 4), 0):    # do stuff with number`
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1606
Joined: Thu Feb 07, 2013 3:42 pm

### Re: How to loop until a certain condition happens?

a modification of stranacs example but not using break
Code: Select all
`import randomnumber = 1while number != 0:    number = random.randint(0, 4)    # do stuff with number`
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam

metulburr

Posts: 1947
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

### Re: How to loop until a certain condition happens?

Note that the two while examples are not the same.
metulburr's will end after using 0.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1606
Joined: Thu Feb 07, 2013 3:42 pm

### Re: How to loop until a certain condition happens?

to answer stranacs recent criticism:
Code: Select all
`import randomnumber = random.randint(0, 4)while number != 0:    # do stuff with number    number = random.randint(0, 4)`
New Users, Read These Hints

Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap

Posts: 658
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

### Re: How to loop until a certain condition happens?

Thanks heaps for the help guys, very muchly appreciated
billygo

Posts: 4
Joined: Wed Mar 26, 2014 9:50 am