English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

OK yes I’m an idiot…
Here’s the part of the code I’m having trouble with:

for ($cointoss = rand(1,2); $toss = $toss;) {

if ($cointoss = 1) {
$heads = 1;
print "Number of heads, $heads
";
}

else {
$tails = 2;
print "Number of tails, $tails
";
}

}

//end of my crapy code...

I’ve already set the $toss in the form. It’s giving me an infinite loop or whatever. What I’m tryin to do is have the user type in the number of tosses they want it to do (which is going to be defined as $toss) and then have it count how many heads and tails in total and out put it back to the user. Like “Number of heads, 10” “Number of tails, 10” “You flipped the coin 20 times”. That kinda thing.

Please don’t give me the answer straight up, it’s for class I don’t want to cheat. What I want is some help as to what the hell I’m doing wrong. The teacher tried explaining it to me, but I didn’t get it and I REALLY had to be somewhere so I didn’t ask for a clarification. PLEASE someone help me!!!!!!

2007-02-26 07:35:30 · 5 answers · asked by Am 4 in Computers & Internet Programming & Design

Now I'm getting:
"Parse error: syntax error, unexpected T_WHILE in C:\Inetpub\wwwroot\lab4.php on line 38
"

Here's my code now:




if (empty($toss)) {

print <<








Coin Toss Simulator



HERE;
}



else {

$cointoss = rand(1,2);
$atoss = 0

while ($atoss < $toss;) {
$atoss++;

if ($cointoss == 1) {
$heads = 1;
print "Number of heads, $heads
";
}

else {
$tails = 2;
print "Number of tails, $tails
";
}

}


print <<
flip coin again


HERE;
}




?>

2007-02-26 08:13:04 · update #1

I changed it to a while ... and I tried out the == ...
but yeah...

Um, I'll try what that dude said about just making it count 10 flips, maybe if I get it to do that then I can figure out what I NEED it to be doing ... I'll post up the stuff again soon ... thx 4 the help ;-)

2007-02-26 08:14:32 · update #2

OR ...

What about:

/*
changed again but now it's back at a never ending loop AHhhhhhhh!
*/


else {

for ($cointoss = rand(1,2); $toss == $toss;) {

if ($cointoss = 1) {
$heads = 1;
$heads++;
print "Number of heads, $heads
";
}

else {
$tails = 2;
$tails++;
print "Number of tails, $tails
";
}

}

// end of code

2007-02-26 08:19:07 · update #3

ok I'll brb I'm gonna make it just freakin count ...

2007-02-26 08:20:02 · update #4

5 answers

Your for loop is infinite because you need a double equals for the $toss=$toss (it should say if $toss == $toss) part of your for loop. You also need == for the if statement. A single equals is for assignment which means it returns true in a condition if it does not create an error. Also without doing something to toss inside that for loop it will either not run at all, or run infinitely.

Hope that helps

RJ

2007-02-26 07:46:15 · answer #1 · answered by Anonymous · 2 0

Try creating your for loop to just print 1 through 10 first. Then inside your loop, add your coin toss logic.

2007-02-26 15:44:52 · answer #2 · answered by javier 2 · 2 0

In the for loop you are setting toss equal to toss every iteration. You are also setting cointoss equal to 1 in the if block. Remeber that = is assinging a value == is testing a value

2007-02-26 15:49:08 · answer #3 · answered by Anonymous · 1 0

1) your for condition is not correct. It should look like
for ( initialize a counter; conditional statement; increment a counter)
2) Your code will always print out 1 for heads and 2 for tails, no matter how many times you flip the coin.

2007-02-26 15:50:34 · answer #4 · answered by David V 5 · 0 0

Maybe this class is teaching a for loop, but I always prefer a while loop. Something like while($currenttoss < $numberoftosses){
LOGIC
$currenttoss++;
}

2007-02-26 15:55:32 · answer #5 · answered by Clinton G 2 · 0 0

fedest.com, questions and answers