## 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: 1790
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`
we will be moving to python-forum.io on October 1 2016
more details here

metulburr

Posts: 2244
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: 1790
Joined: Thu Feb 07, 2013 3:42 pm

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

Code: Select all
`import randomnumber = random.randint(0, 4)while number != 0:    # do stuff with number    number = random.randint(0, 4)`
Due to the reasons discussed here we are moving to python-forum.net on October 1, 2016.

This forum will be closed. Please create an account at the new site to continue discussion.

IRC://irc.freenode.net/python-forum
Kebap

Posts: 689
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

### Who is online

Users browsing this forum: No registered users and 14 guests