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

566 points
512e079b4de69820720049d0_487406516
Submitted by
Benjamín Romero
over 5 years ago

Oops, try again! The random output you printed from your name should be just one character

I don't know why my code is not working. I just receive this warning: Oops, try again! The random output you printed from your name should be just one character. But I can see the correct result on the console.
I have spent a couple of hours and I don't know. Thanks for your help.

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

echo "A number between 2 and 10 is ";
echo rand(2, 10);

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

$name = "Benjamin";
echo "A character number of $name is of ";
echo strlen ($name);


echo "The name characters for $name between the position of 2 and 4 is ";
echo substr($name, 2, 4);

$random = rand(0, strlen ($name)-1);
$alea = substr($name, $random, 1);
//echo substr($name, $random, $random1];
echo $name[$alea];
?>

60 votes

permalink

I spent much time on this problem and this is what I came up with. I remembered that rand() requires two parameters and substr() takes three. So you have to think. You're looking for one character in your name -- which you should store in a variable I.E. $myName -- so you want to use substr() first. Remember that this takes three parameters. The first parameter in substr() represents the actual string. So if your stored your name in a variable, it would be
$answer = substr(variable, ..., ...);. The next parameter is the starting point. Remember that substr() treats characters in a string as a zero-indexed array. What this means is that the first letter in your string (which should be stored in a variable) is positioned at zero. So that's where we want to start our random character. So you want to put rand(0, ...) inside of your substr() so now we should have $answer = substr(variable, rand(0, ...), ...); Now for the second parameter of rand() (because it does take two) we need to have the maximum number. If we think about it, the final letter in your string will be the length minus 1. This is because with a zero-indexed array, each letter is minus 1 from where you'd count it at. So in my name -- Chris -- there are 5 letters. In an array, the numbers would be positioned at array[0, 1, 2, 3, 4]; So yes, strlen() will return 5, but with substr() in there, the final letter would be at 5 - 1. So if you followed all of that, now we should be at

$answer = substr(variable, rand(0, strlen(variable)-1, ...). And finally, the final parameter in our substr() function. The final parameter is reserved for how many characters you'd like to return. This lesson is asking for only one, so it is safe to assume that we're going to put a one there. The final code you should end up with should look something similar to
$answer = substr(variable, rand(0, strlen(variable)-1, 1); Then of course you want to print/echo it, so after the semi-colon, put echo $answer;. My final code looked like this:

<html>
<p>
<?php
 //Use rand() to print a random number to the screen
    echo floor(rand(5, 15));
?>
</p>
<p>
<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
  $name = "Chris";
  $print = substr($name, rand(0, strlen($name) - 1), 1);
    echo $print;
?>
</p>

1116 points
5101093403f5418ca8002e4c_132261751
Submitted by
Chris McKay
over 5 years ago

35 Comments

512e079b4de69820720049d0_487406516 Benjamín Romero over 5 years ago

That's great. It worked!! :) Thanks

5e9b47c973ad74b94b1b445956d4cdc5?s=140&d=retro courseRockstar68051 over 5 years ago

Great answer with a great explanation. Thanks.

5101093403f5418ca8002e4c_132261751 Chris McKay over 5 years ago

No problem, Glad I could help!

Picture Taito Llama over 5 years ago

Fantastic explanation, thanks!

5101093403f5418ca8002e4c_132261751 Chris McKay over 5 years ago

No problem! :)

Picture Michiel Welling over 5 years ago

thanks mate, this was very helpful. btw, is it just me or does anyone have problems with this site? it crashes all the time.

516b1237636b96785f0026af_567534798 Stacey over 5 years ago

Thank you a ton for this thorough explanation!

5101093403f5418ca8002e4c_132261751 Chris McKay over 5 years ago

No problem :)

51e03305631fe91d5f000501_619796379 Vikram Nithyanandam about 5 years ago

great

5101093403f5418ca8002e4c_132261751 Chris McKay about 5 years ago

Thanks! :)

Picture Mircea Rojnita about 5 years ago

it works , thanks a lot

5101093403f5418ca8002e4c_132261751 Chris McKay about 5 years ago

No problem :)

44dbe2db3bd53c3dbb7e57d8f163e120?s=140&d=retro optimizer about 5 years ago

thanks, you save my day :D

5101093403f5418ca8002e4c_132261751 Chris McKay about 5 years ago

I'm glad I could help! :)

520b03ef80ff332447001d19_311611358 Vishal Shira about 5 years ago

thank you

5249ddfdabf821e459000ae1_960197475 matt raney almost 5 years ago

You're a good man for explaining out the code so thoroughly. Thanks

72adaf6c266e171f56e4c027e1808232?s=140&d=retro AjayMartin almost 5 years ago

Thank you Chris

Fe8761f13702392e449a82f97ca4a44c?s=140&d=retro nagendrababu865 almost 5 years ago

good

Fe8761f13702392e449a82f97ca4a44c?s=140&d=retro nagendrababu865 almost 5 years ago

thank u!

5261b4f980ff33b419000a66_994406302 Ekom Otu almost 5 years ago

Thanks man, u save me 200days of confusion

527c8641f10c603459002130_732188931 Roger Lopez almost 5 years ago

I never thought of that. Thanks!

3aab6c1b5011f8933740817ec1ce5763?s=140&d=retro Codeicidal almost 5 years ago

Very helpful thank you

5ba33235aa33d1297b0002b5_337504979 tutunja almost 5 years ago

THANK YOU SO SO MUTCH!!!

Picture Carlos E. Guillen almost 5 years ago

Thank you bro, that really help. Just like @Roger Lopez never though of merge everything in one sentence.

52cd6075631fe9c776001d5f_535842939 Fernanda Lima almost 5 years ago

Thanks! Very good.

54bcff9476b8feb5820060f8_754158015 Robyn-Dale Samuda over 4 years ago

This worked very well. Excellent

52be604780ff33a7e80043ff_480808542 Paramanathan ilangeeran over 4 years ago

Thank you...

530afb769c4e9d5efb001169_354986657 deekiarie over 4 years ago

it worked

8c4fcbfb3c2e42ee21327b329eb125d9?s=140&d=retro CM13 over 4 years ago

Great explanation, very helpful. Cheers.

5130aa69ed1c9aa0b20094fb_862123275 Pranit over 4 years ago

Thanks bro , good answer ! :-)

383fd3a923f72ae9052ab4fdf5238aed?s=140&d=retro Тимур Сарсембаев over 4 years ago

useful, thanks

53b138568c1cccb525004334_60094006 Anton Boksha about 4 years ago

Not working for me :(
$name = "Anton";
echo $name;
$count = strlen($name);
echo "Name-length is: $count";
//echo substr($name, 0, $count);
$random = substr($name, rand(0, $count - 1),1);
print "$random";

Picture Paul Rosenbeck about 4 years ago

You saved the day, I was about to give up, Thanks!

Picture Day Will Come over 3 years ago

But in this case we have first character, not the random one. Right?

1fd1601a6654f4f1164c8bc7b19f2358?s=140&d=retro Bryan over 3 years ago

Thanks.


8 votes

permalink

Simplify! The fewer lines of code, the better.

<html>
<p>
<?php
// Use rand() to print a random number to the screen
print rand(1, 10);
?>
</p>
<p>
<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$myName = "Josiah";
$sub1 = substr($myName, rand(strlen($myName) - 1), 1);
print $sub1;
?>
</p>

1577 points
Picture
Submitted by
Josiah
over 5 years ago

1 Comment

Bcf288408ad8bfb1803366378ceef172?s=140&d=retro Petsky about 5 years ago

Hi Josiah, rand() should have 2 parameters, maybe you lack comma after strlen($myName) =)


4 votes

permalink

Spanish:Hola Benjamin, gracias a tu codigo he conseguido que funcione el mio, yo lo hice asi:
English: Hi Benjamin, thanks to your code I got mine working fine, here it is:

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


<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "Bruno";
$character = rand(0,strlen($name)-1);
$letter = substr($name,$character,1);
print $letter;
?>

1603 points
Ff29e3dbfa72ea50085007883c285c98?s=140&d=retro
Submitted by
bruno celaya
over 5 years ago

2 Comments

512e079b4de69820720049d0_487406516 Benjamín Romero over 5 years ago

Hi Bruno. Good eye to know I speak Spanish :)
Unfortunately, your code isn't work on my console neither. I don't know what's go on.. I tried in other browsers as well but it doesn't work

Cf261f89c91bd137002ccc925974004b?s=140&d=retro ChrisGer1669_53504164548c3573ec00008 over 4 years ago

Bruno muchas gracias por la solucion hermano, por mas vueltas que le daba no encontraba la forma de solucionar el ejercicio.


3 votes

permalink

This code works:

<?php
// Create an array and push on the names
// of your closest family and friends
$array = array("a", "b", "c");
arraypush($array, "d");
array
push($array, "e");
array_push($array, "f");
// Sort the list
sort($array);
// Randomly select a winner!
$winner = rand(0, count(sort($array)) - 1);
// Print the winner's name in ALL CAPS
print strtoupper($array[$winner]);
?>

817 points
8f7066785936a3dc0e39731d10361b0a?s=140&d=retro
Submitted by
Bine Intentionat
about 5 years ago


1 vote

permalink

<?php
// Use rand() to print a random number to the screen
$random = rand(0,5);
print $random;
?>
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "Adriano";
$character = rand(0,strlen($name)-1);
$letter = substr($name,$character,1);
print $letter;
?>

why isnt this working?
it says The random output you printed from your name should be just one character.

929 points
9f48d9ff053523f3212399009c58820f?s=140&d=retro
Submitted by
AdrianoC
over 5 years ago

2 Comments

512e079b4de69820720049d0_487406516 Benjamín Romero over 5 years ago

It's my same problem and it everything fine apparently

Ff29e3dbfa72ea50085007883c285c98?s=140&d=retro bruno celaya over 5 years ago

Try to refresh adriano...press ctrl and r or command and r simultaniously...is a bug there in the codeacademy editor and sometimes dont check right the code...if u still having problems please tell me


1 vote

permalink

Thanks for the explanation, was very helpful, but i have a small misunderstanding. I writte my code like this:

<?php
// Use rand() to print a random number to the screen
$random = rand(0,6);
echo $random;
?>


<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$myname = "Viorel";
$random = substr("Viorel", rand(0, strlen($my
name) - 1), 1);
echo $random;
?>

and it works fine, but if a use the variable "myname" in the substr function it give's me errors! Might be a console problem or i missed something? Thank you very much!

602 points
Picture
Submitted by
Popa Viorel
over 5 years ago


1 vote

permalink

Hi all,
this exercise requires the use of strlen(), substr() and rand().
However a simple, clean and working solution that doesn't involve substr() is:

<html>
<p>
<?php
// Use rand() to print a random number to the screen
print rand(0,5);
?>
</p>
<p>
<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "myname";
print ($name[rand(0, strlen($name-1))]);
?>
</p>

944 points
Bdbaca4bfd9079aa9b5c35a54ad55a23?s=140&d=retro
Submitted by
Luca
almost 5 years ago

1 Comment

Picture Manaz Ahmed over 3 years ago

its working..


1 vote

permalink

<?php
echo rand();
?>


<?php
print substr( "Anthony", rand( 0, strlen( "Anthony" ) - 1 ), 1 );
?>

328 points
51a311b7ab7e125ba0002382_272914958
Submitted by
Anthony Bryant
almost 5 years ago


1 vote

permalink

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

$leng = strlen($name);

$rannum2 = $leng - rand(1,5);

print substr($name, $rannum2 , 1);

?>

70 points
5333dbd79c4e9da61a0013ec_46559296
Submitted by
Aumkar Thakur
over 4 years ago


0 votes

permalink

thanks for the help, I was kinda close, yet so far away :)

1374 points
Bdc303e4b8d72f5897335057d7c8a768?s=140&d=retro
Submitted by
Jeff Shamley
over 5 years ago

6 Comments

52565a96abf821641d001a8f_683238750 Kamalrajsinh Sodha almost 5 years ago

still the problem is same as above after refreshing also

Bdbaca4bfd9079aa9b5c35a54ad55a23?s=140&d=retro Luca almost 5 years ago

use this:
print ($name[rand(0, strlen($name-1))]);

52565a96abf821641d001a8f_683238750 Kamalrajsinh Sodha almost 5 years ago

The output on the console is true but then also it displays the error.

Bdbaca4bfd9079aa9b5c35a54ad55a23?s=140&d=retro Luca almost 5 years ago

Write your code down so I can check it; do it in a post and not in a comment please

52565a96abf821641d001a8f_683238750 Kamalrajsinh Sodha almost 5 years ago

Check the post, I have posted it

Bdbaca4bfd9079aa9b5c35a54ad55a23?s=140&d=retro Luca almost 5 years ago

can't see you post yet. sometimes it takes time to show.
In the meanwhile check the solution I've posted yesterday on the bottom of this thread


0 votes

permalink

Benjamin...solucionastes el problema? Si no es asi dime algo a [email protected] e intentare ayudarte en todo lo posible.
pd:me gusto mucho tu web

1603 points
Ff29e3dbfa72ea50085007883c285c98?s=140&d=retro
Submitted by
bruno celaya
over 5 years ago

1 Comment

512e079b4de69820720049d0_487406516 Benjamín Romero over 5 years ago

Gracias Bruno. Si lo acabo de solucionar tras entender la gran explicacion que Chriss Mackay me ha hecho. Mucho animo. Imagino que estas enganchado a codecademy como yo :)


0 votes

permalink

<?php
// Use rand() to print a random number to the screen
$sohil = rand(1,9);
print $sohil;
?>


<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "sohil";
$sub1 = substr($name, rand(0,strlen($name) - 1), 1);
print $sub1;
?>

89 points
52f9e79b80ff3371d50017a1_623110757
Submitted by
Sohil Sharma
over 4 years ago


0 votes

permalink

I have the following code and it does not work on the editor

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


<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "rakesh";
$slen = strlen($name) -1;
print $slen."";
$pr = substr($name, rand(0, strlen($name) - 1), 1);
print $pr."";
$sstr = substr($name,3,1);
print $sstr."";
?>

374 points
B9773c32a9aca9a94db3f7a399aa1be0?s=140&d=retro
Submitted by
rockysreenivasa
about 4 years ago


0 votes

permalink

SOrry none of the answers help in the case...this is the msg i am getting Oops, try again. Your printed output from rand() should be an integer.

103 points
Picture
Submitted by
biggig
almost 4 years ago

1 Comment

F52f71c1851fc4da241da9a37df5d84f?s=140&d=retro tenzinchoenyi almost 4 years ago

thats the correct answer
just try this'


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


<?php
$name ="choenyi";
$random = rand(0,strlen($name)-1);
$char =$name[$random];
echo $char;
// gets the length of the
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.

?>
</p>

0 votes

permalink

<?php

$name="Esac";
echo substr($name,rand(0,strlen($name)-1),1);

?>+

264 points
3e9ad9875e7a55bc0e8d75d5054fc4cf?s=140&d=retro
Submitted by
c0rpse
almost 4 years ago


0 votes

permalink

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


<?php
$name ="choenyi";
$random = rand(0,strlen($name)-1);
$char =$name[$random];
echo $char;
// gets the length of the
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.

?>
</p>

377 points
F52f71c1851fc4da241da9a37df5d84f?s=140&d=retro
Submitted by
tenzinchoenyi
almost 4 years ago

1 Comment

F52f71c1851fc4da241da9a37df5d84f?s=140&d=retro tenzinchoenyi almost 4 years ago

this is correct answer for printing the random character


0 votes

permalink

<?php
// Use rand() to print a random number to the screen
echo rand(0,10);

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

$myName = "Manish";
$sub1 = substr($myName, rand(strlen($myName) - 1,strlen($myName) - 3), 1);
print $sub1;

?>
</p>

282 points
F18fd00d1f177ebc3049005353113eb2?s=140&d=retro
Submitted by
gomeetmanish_54802adc8c1ccc5ec3006dc
over 3 years ago


0 votes

permalink

Simply do :-

$name = "Pravvy";
$len = strlen($name);
$rand = rand(0, $len - 1);
echo $name[$rand];

513 points
Picture
Submitted by
Pravash Panigrahi
over 3 years ago