This forum is now read-only. Please use our new forums at discuss.codecademy.com.

While Loops in PHP Forum

View Course » View Exercise

889 points
Picture
Submitted by
Scott Tiernan
over 4 years ago

Errors running do/while loop

Here's my code I'm trying to run:

<!DOCTYPE html> <html>
    <head>      <title>Your own do-while</title>
        <link type='text/css' rel='stylesheet' href='style.css'/>   </head>     <body>
    <?php
        //write your do-while loop below
        $roll = rand(1,6);
        $count = 1;
        $spc = " ";
        echo ($roll);
        $last = $roll;
        do{
            $roll = rand(1,6);
            echo ($roll);
            $count +=
        }while $roll!=$last;

        echo ("<p>It took ($count) rolls to roll the same number twice.</p>");
    ?>
    </body> </html>

I keep getting unexpected } errors on the bracket before while; however, I believe that is where it should go. Help? Thanks!


6 votes

permalink

<?php
    $loopCond = false;
    do {
    echo "<p>The loop ran even though the loop condition is false.</p>"; 
    while ($loopCond);
    echo "<p>Now the loop is done running.</p>"; 
$roll = rand(1,6);
$count = 1;
$spc = " ";
echo ($roll);
$last = $roll;
do{
$roll = rand(1,6);
echo ($roll);
$count ++;
}
while ($roll != $last);
echo "It took {$count} rolls to roll the same number twice.";
?>

1113 points
530269e680ff339528000763_329684184
Submitted by
Danen
over 4 years ago


2 votes

permalink

Look at this did does the trick:

$roll = rand(1,6);
$count = 1;
$spc = " ";
echo ($roll);
$last = $roll;
do{
$roll = rand(1,6);
echo ($roll);
$count ++;
}
while ($roll != $last);
echo "It took {$count} rolls to roll the same number twice.";

334 points
18aa70a7dd8fd44e68e4cc015123126b?s=140&d=retro
Submitted by
Kelly Rijksen
over 4 years ago

1 Comment

Picture Scott Tiernan over 4 years ago

I was so close! Thanks, Kelly!


1 vote

permalink

Actually didn't need it. Turned out to be simple, like all the others - forgot camel case on $loopCond.

Catch you on FF!

784 points
Ad96ace475ab89952489d5a2454d97da?s=140&d=retro
Submitted by
Mary Baum
over 4 years ago


1 vote

permalink

f h r in the pusy

43 points
63154703ec52e5432891d90dc7adac14?s=140&d=retro
Submitted by
1plus419
about 3 years ago

1 Comment

4eab6b69bf6d8c2baa3939e443145d8a?s=140&d=retro mbowl10 over 2 years ago

yeah


0 votes

permalink

I'm having all kinds of trouble dreaming up acceptable conditions. May I borrow yours?

784 points
Ad96ace475ab89952489d5a2454d97da?s=140&d=retro
Submitted by
Mary Baum
over 4 years ago


0 votes

permalink

Quick question. What does the $spc = " "; do in your code?

611 points
1bec61b6f0eefe3e64237759f6940d0c?s=140&d=retro
Submitted by
cwn90
over 3 years ago

1 Comment

Picture Scott Tiernan over 3 years ago

I'm guessing the original developer had that in there to insert a space...but it's just a guess.


-2 votes

permalink

Hey, Bubba!
Mary

784 points
Ad96ace475ab89952489d5a2454d97da?s=140&d=retro
Submitted by
Mary Baum
over 4 years ago