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

Functions, Part I Forum

View Course » View Exercise

1133 points
13fa684521d6cc9183e26dab2a471400?s=140&d=retro
Submitted by
tanpinche_coursera
over 2 years ago

"Your printed output from rand() should be an integer."

I thought that we were supposed to print the letter, not the number. Can somebody explain what's going on? Even when I print the number it still says I'm going wrong. `

<?php
// Use rand() to print a random number to the screen

?>
</p>
<p>
<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$my_name = "Tan Pin Che";
$length = strlen($my_name);
$ch = rand(0, $length - 1);
echo $ch;
// print substr($my_name, $ch, 1);
?>
</p>

`


3 votes

permalink

Hi Tanpinche,

The 2 blank spaces in your full name will occasionally generate an error as they expect a character (letter).
Error message: "Oops, try again. The random output you printed from your name should be just one character."

Delete echo $ch; from your code as there will be an error message "Oops, try again. The random output you printed from your name should be a letter." The result is a number and letter, ie 10e, 5i, 9h etc when you print substr.

Also, remove // from your last line of code.

As shown below:

  • Part 1

<?php
// Use rand() to print a random number to the screen
print rand(1,100);
?>

  • Part 2 & 3

<?php
$myname = "TanPinChe";
$length = strlen($my
name);
$ch = rand(0, $length - 1);
print substr($my_name, $ch, 1);
?>

968 points
Ea1415d55822154ba5a5b2ad2d6a16c5?s=140&d=retro
Submitted by
Saburi Yu
about 2 years ago

1 Comment

8fed338af3dbbf82959e77c67ed44d96?s=140&d=retro lightbrute almost 2 years ago

not working, there is something wrong with the code checker


2 votes

permalink

<?php
// Use rand() to print a random number to the screen
print rand(1,5);
?>


<?php
$name = "Sileye";
$length = strlen($name);
$ch = rand(0, $length - 1);
print substr($name, $ch, 1);
?>

1022 points
A6f078689e81d6468f7a991250f0e48b?s=140&d=retro
Submitted by
Sileye Sarr
about 2 years ago

1 Comment

A5ba1285e8142aa3740cc08378e41d6c?s=140&d=retro Reianna-Bryant almost 2 years ago

What does the "$ch" stand for, and how does it fit into the lesson? It was not explained in the information bow..


1 vote

permalink

<?php // Use your knowledge of strlen(), substr(), and rand() to print a random character from your name to the screen.

$myname = "Tan Pin Che";
$length = strlen($my
name);
$ch = rand(0, $length - 1);
//echo $ch;
print substr($my_name, $ch, 1); ?>

679 points
525a08f67b060d1a19027e331c569a1c?s=140&d=retro
Submitted by
Anasalikhan
over 2 years ago

1 Comment

5c792144e7815680437e45ceade88d70?s=140&d=retro sergpr about 2 years ago

my_name should replace to myname


1 vote

permalink

Yeah, this is a bug with codecademy. I've tried multiple working solutions, everything returns "Your printed output from rand() should be an integer." I'm done screwing around with this.

The PHP tutorial has been noticeable lower quality than the others at codecademy. The interpreter is constantly crashing, probably because it's live-interpreting the input with every new character you type in. They're are other bugs too... did they even test this one?

574 points
Bf714ffa8a050765ce0384b76a1d3af1?s=140&d=retro
Submitted by
ideascape
over 2 years ago

2 Comments

7d0bc21d8eca0ff5db59705b8dae3432?s=140&d=retro CoosWolff about 2 years ago

couldn't agree more. It annoys me so much

D74de959b7f708e18a136b7591f37ea2?s=140&d=retro Gavin Hardison about 2 years ago

im a completionist and this 99% progress will annoy me


-1 votes

permalink

So sorry. I missed the part where it said to print a random number.

1133 points
13fa684521d6cc9183e26dab2a471400?s=140&d=retro
Submitted by
tanpinche_coursera
over 2 years ago