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?

Postby billygo » Wed Mar 26, 2014 10:01 am

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 = 0
import 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

Postby stranac » Wed Mar 26, 2014 10:44 am

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.
User avatar
stranac
 
Posts: 1155
Joined: Thu Feb 07, 2013 3:42 pm

Re: How to loop until a certain condition happens?

Postby metulburr » Thu Mar 27, 2014 5:34 am

a modification of stranacs example but not using break
Code: Select all
import random
number = 1
while 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
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: How to loop until a certain condition happens?

Postby stranac » Thu Mar 27, 2014 6:59 am

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.
User avatar
stranac
 
Posts: 1155
Joined: Thu Feb 07, 2013 3:42 pm

Re: How to loop until a certain condition happens?

Postby Kebap » Thu Mar 27, 2014 9:23 am

to answer stranacs recent criticism:
Code: Select all
import random
number = random.randint(0, 4)
while number != 0:
    # do stuff with number
    number = random.randint(0, 4)
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: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: How to loop until a certain condition happens?

Postby billygo » Sun Mar 30, 2014 11:55 pm

Thanks heaps for the help guys, very muchly appreciated
billygo
 
Posts: 4
Joined: Wed Mar 26, 2014 9:50 am


Return to General Coding Help

Who is online

Users browsing this forum: snippsat and 2 guests