473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Simple if/else problem.

13
I'm trying to make a really really really basic rpg. This is how it starts.

Unfortunately, I seem to have gotten my if/elsif statements mixed up. Because whatever option you choose, it always prints 'LOLOL'

I'm not sure why.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #!/usr/bin/perl
  4.  
  5. $left = 'left';
  6. $right = 'right';
  7.  
  8. print "You\'re walking along, and you see a fork in the road. What do you do\?.\n";
  9.  
  10. print "Turn left\?\n";
  11.  
  12. print "Turn right\?\n";
  13.  
  14. $choice = <stdin>;
  15. chomp $choice;
  16.  
  17. if ($choice == $left) {
  18. print "LOLOL"; 
  19. }
  20. elsif ($choice == $right) {
  21. print "HAHA";
  22. }
  23.  
  24. else {
  25.  print "PRINT A VALID OPTION MAAAAAN";
  26. }
  27.  
  28.  
Sep 6 '07 #1
7 1773
numberwhun
3,509 Expert Mod 2GB
I'm trying to make a really really really basic rpg. This is how it starts.

Unfortunately, I seem to have gotten my if/elsif statements mixed up. Because whatever option you choose, it always prints 'LOLOL'

I'm not sure why.
Well, your script has a few issues with it that you needed to correct. First, you should always "use warnings;" and "use strict;". Doing so would have shown you a slew of errors because you didn't define your variables with 'my'. After correcting that, you would have received the error that you were doing a numeric comparison (==) when you should be doing a string comparisong (eq). This is something you need to be careful of.

Plus, when coding, be sure that if you are wanting your users to make a choice, let them know what valid options are. If your users type in "Turn left", the script will not work, even though that is what you asked them for.

Here is a version of your script that works, with some improvements. I also think that you should take a read of the perlop page on perldoc. It will explain the difference(s) between == and eq and many others.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $left = 'left';
  7. my $right = 'right';
  8.  
  9. print "You\'re walking along, and you see a fork in the road. What do you do\?.\n";
  10.  
  11. print "To Turn Left(type left)\?\n";
  12.  
  13. print "To Turn Right(type right)\?\n";
  14.  
  15. my $choice = <STDIN>;
  16. chomp $choice;
  17.  
  18. if ($choice eq $left) {
  19. print "LOLOL";
  20. }
  21. elsif ($choice eq $right) {
  22. print "HAHA";
  23. }
  24.  
  25. else {
  26. print "PRINT A VALID OPTION MAAAAAN";
  27. }
  28.  
Sep 6 '07 #2
I'm trying to make a really really really basic rpg. This is how it starts.

Unfortunately, I seem to have gotten my if/elsif statements mixed up. Because whatever option you choose, it always prints 'LOLOL'

I'm not sure why.
I think that the if loop is fine but there is a problem with the variable $left and $right being strings and the test being == rather than eq. I think that this works although my reasoning may be incorrect.

cheers

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. $left = "left";
  4. $right = "right";
  5.  
  6. print "You\'re walking along, and you see a fork in the road. What do you do\?.\n";
  7.  
  8. print "Turn left\?\n";
  9.  
  10. print "Turn right\?\n";
  11.  
  12. $choice = <stdin>;
  13. chomp $choice;
  14.  
  15. if ($choice eq $left) {
  16. print "LOLOL";
  17. }
  18. elsif ($choice eq $right) {
  19. print "HAHA";
  20. }
  21. else {
  22. print "PRINT A VALID OPTION MAAAAAN";
  23. }
  24.  
Sep 6 '07 #3
numberwhun
3,509 Expert Mod 2GB
I think that the if loop is fine but there is a problem with the variable $left and $right being strings and the test being == rather than eq. I think that this works although my reasoning may be incorrect.

cheers

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. $left = "left";
  4. $right = "right";
  5.  
  6. print "You\'re walking along, and you see a fork in the road. What do you do\?.\n";
  7.  
  8. print "Turn left\?\n";
  9.  
  10. print "Turn right\?\n";
  11.  
  12. $choice = <stdin>;
  13. chomp $choice;
  14.  
  15. if ($choice eq $left) {
  16. print "LOLOL";
  17. }
  18. elsif ($choice eq $right) {
  19. print "HAHA";
  20. }
  21. else {
  22. print "PRINT A VALID OPTION MAAAAAN";
  23. }
  24.  
First, if you are going to step on my post, then please be kind enough to include code tags around your code.

Second, please, DO NOT top post. It is annoying to everyone who reads your posting because.....

say.
to
trying
are
you
what
tell
to
able
be
won't
they

I have fixed both of these issues in the post that you provided.

Regards,

Jeff
Sep 6 '07 #4
Sorry about that Jeff. This was the first question that I have come across on the forum that I could actually be of some help on! When I started to answer it there were no replies and after I posted I saw yours.


Cheers

Alan
Sep 6 '07 #5
numberwhun
3,509 Expert Mod 2GB
No worries. :-)

Regards,

Jeff
Sep 6 '07 #6
gloomer
13
Why the heck do you need to put 'my' in front of all the variables?

You can't just define variables?
Sep 6 '07 #7
numberwhun
3,509 Expert Mod 2GB
Why the heck do you need to put 'my' in front of all the variables?

You can't just define variables?
Sure, you can just define variables, if you don't "use strict;". The strict pragma forces you to define your variables with my before or when they are first used. This will further explain the strict pragma for you.

Regards,

Jeff
Sep 7 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Bruce W...1 | last post by:
In my effort to learn PHP I'm playing with some simple email scripts. They worked a few days ago but they stopped working. The only thing I've done to this Windows 2000 PC in this time was a...
27
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here....
1
by: elastic | last post by:
I'm trying to implement a simple timer to work on win32 (98,2k,xp), solaris and linux. where the last two are simple, the first seems to be problematic. the problem is that the kill command seems...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
0
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I...
5
by: Tim::.. | last post by:
Can someone tell me how I convert this simple SQL statement so I can use it in ASP.NET??? I have an issue with the quotation marks and wondered if there is a simple rule for converting the sql...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
6
by: sathyashrayan | last post by:
Dear group, Following is a exercise from a book called "Oreilly's practical C programming". I just wanted to do a couple of C programming exercise. I do have K and R book, but let me try some...
26
by: jacob navia | last post by:
Summary: I have changed (as proposed by Chuck) the code to use isalpha() instead of (c>='a' && c <= 'z') etc. I agree that EBCDIC exists :-) I eliminated the goto statement, obviously it is...
11
by: James R. Davis | last post by:
Yes, a newbie here. Though I am making progress, slowly, I am also getting more and more confused. With ASP, when I wanted to do something as trivial as updating a visitor counter, I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.