# it can't tell the difference

• Beginner
• Posts: 51

3+ Months Ago

i made a cheesy little game. which, if you guess the same number the computer "picks" you win. when i pick the right number it sais i get it and i get it wrong. i use "eq,ne != ==,==" symbols but it won't make a difference. thanks for the help

Code: [ Select ]

print "this is a game of chance";
print "pick a number";

\$number1 = <stdin>;
\$number = 1; #i just put a 1 when its supposed to be random
print "you picked \$number1";

print "the computer picked \$number";

if(\$number == \$number1) { print "goodjob you won"; }
if(\$number ne \$number1) { print "don't worry this is a hard game"; }
1. print "this is a game of chance";
2. print "pick a number";
3. \$number1 = <stdin>;
4. \$number = 1; #i just put a 1 when its supposed to be random
5. print "you picked \$number1";
6. print "the computer picked \$number";
7. if(\$number == \$number1) { print "goodjob you won"; }
8. if(\$number ne \$number1) { print "don't worry this is a hard game"; }
• Web Master
• Posts: 4580
• Loc: Maryland

3+ Months Ago

What language is this?
• Beginner
• Posts: 51

3+ Months Ago

this code is for PERL

i redid my code so it recognizes if the numbers are equal or not but when i put in the right number it doesn't print anything. my new code is this

Code: [ Select ]
print "this is a game of chance";
print "pick a number";

\$number1 = <stdin>;
\$number = 1;
print "you picked \$number1";

print "the computer picked \$number";

if(\$number eq \$number1) { print "goodjob you won"; }
if(\$number != \$number1) { print "don't worry this is a hard game"; }
1. print "this is a game of chance";
2. print "pick a number";
3. \$number1 = <stdin>;
4. \$number = 1;
5. print "you picked \$number1";
6. print "the computer picked \$number";
7. if(\$number eq \$number1) { print "goodjob you won"; }
8. if(\$number != \$number1) { print "don't worry this is a hard game"; }
• Web Master
• Posts: 4580
• Loc: Maryland

3+ Months Ago

Why not try elseif for the second if statement, and then instead of setting \$number1 to stdin, set it to 1, and see if you get the correct result. I also would recommend sticking to == and !=
• Professor
• Posts: 774
• Loc: Brisbane, Australia

3+ Months Ago

perl doesn't use !=, instead use <>

IMO the best way is this:
Code: [ Select ]
print "this is a game of chance";
print "pick a number";

\$number1 = <stdin>;
\$number = 1; #i just put a 1 when its supposed to be random
print "you picked \$number1";

print "the computer picked \$number";

if(\$number == \$number1) { print "goodjob you won"; }
else
{ print "don't worry this is a hard game"; }
1. print "this is a game of chance";
2. print "pick a number";
3. \$number1 = <stdin>;
4. \$number = 1; #i just put a 1 when its supposed to be random
5. print "you picked \$number1";
6. print "the computer picked \$number";
7. if(\$number == \$number1) { print "goodjob you won"; }
8. else
9. { print "don't worry this is a hard game"; }

but to do it your way would be
Code: [ Select ]
print "this is a game of chance";
print "pick a number";

\$number1 = <stdin>;
\$number = 1; #i just put a 1 when its supposed to be random
print "you picked \$number1";

print "the computer picked \$number";

if(\$number == \$number1) { print "goodjob you won"; }
if(\$number <> \$number1) { print "don't worry this is a hard game"; }
1. print "this is a game of chance";
2. print "pick a number";
3. \$number1 = <stdin>;
4. \$number = 1; #i just put a 1 when its supposed to be random
5. print "you picked \$number1";
6. print "the computer picked \$number";
7. if(\$number == \$number1) { print "goodjob you won"; }
8. if(\$number <> \$number1) { print "don't worry this is a hard game"; }

note that I didn't test either of these so I'm not sure whether or not they work.
• Guru
• Posts: 1259
• Loc: ./

3+ Months Ago

Actually, the code Hiei posted last works with one small modification:
Code: [ Select ]
print "this is a game of chance\n";
print "pick a number: ";

\$number1 = <stdin>;
chomp(\$number1);
\$number = 1;
print "you picked \$number1\n";

print "the computer picked \$number\n";

if(\$number == \$number1) { print "good job you won\n"; }
if(\$number != \$number1) { print "don't worry this is a hard game\n"; }
1. print "this is a game of chance\n";
2. print "pick a number: ";
3. \$number1 = <stdin>;
4. chomp(\$number1);
5. \$number = 1;
6. print "you picked \$number1\n";
7. print "the computer picked \$number\n";
8. if(\$number == \$number1) { print "good job you won\n"; }
9. if(\$number != \$number1) { print "don't worry this is a hard game\n"; }

At least - it works for me

This
• Web Master
• Posts: 4580
• Loc: Maryland

3+ Months Ago

That's what I figured. I don't know anything about PERL but I never use anything but == and !=...
• Guru
• Posts: 1259
• Loc: ./

3+ Months Ago

== and != are for the comparison of numbers, to compare text, you would use eq and ne.
The caparison operator (also refered to as the spaceship) <=> is usually used when sorting, but even then <=> is used for numbers, while cmp is used for text, ie:
@sortednums = sort { \$a <=> \$b } @unsortednums;
@sortedtext = sort { \$a cmp \$b } @unsortedtext;

of course, sort's default behavior is to sort by ascii value, so you could do the last as:
@sortedtext = sort @unsortedtext;

Just thought I'd expound on the issue a bit.
Cheers
This
• Web Master
• Posts: 4580
• Loc: Maryland

3+ Months Ago

That is what I figured. I don't think PHP has those, I know it has like XOR and stuff...
• Professor
• Posts: 774
• Loc: Brisbane, Australia

3+ Months Ago

no i'm farily sure that perl uses <> and not !=. but it is much better to use an "else"

edit: wait I was wrong, != is the right one.

## Post Information

• Total Posts in this topic: 10 posts
• Users browsing this forum: No registered users and 50 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum