Role a 6 to win! PHP Code -


i'm learning how use do/while loops in php through codeacademy. i'm working on challenge create six-sided die , continue rolling until six. here's i've come far:

<?php $roll = 1;  { echo “roll number " . $roll . ".  begin rolling 6."; } while ($roll != 6); { ($rollnumber = 1; $roll != 6; $roll ++) { $roll = rand(1, 6); echo "on roll number " . $rollnumber .  "you got  " $roll ".\n"; };} ?> 

i though best way started creating roll variable can use once loop check if it's working:

$roll = 1; 

i set value 1 instead of zero, because there's no 0 number on die, , we'll assume player first rolls one. next want check if loop working echo following:

do { echo “roll number " . $roll . ".  begin rolling 6."; } 

after making sure loop worked, create while condition:

while ($roll != 6) { 

then want create rollnumber variable keep track of roll i'm on , increment it:

for ($rollnumber = 1; $roll != 6; $roll ++) { 

next set value of roll variable generates random number between 1 , 6:

$roll = rand(1,6); 

finally want echo message "on roll number got ." :

echo “on roll number “ . $rollnumber .  "you got  " $roll “.\n";} 

for reason code's not working. suggestions or appreciated! thanks.

you on right track while loop , rand(). other thing needed set count variable display roll number (you can increment variable on every "roll").

your code simplified this:

$c = $n = 1; while ($n != 6) {   echo 'on roll number ' . $c++ . ' rolled ' . $n = rand(1, 6) . "\n"; } 

here's example


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -