ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi! I finally got time to get back to trying to learn perl and I did a number guessing game for practice and understanding and its a exercise to perform out of a book and I was wondering is there far better ways to write this code with only using
if, elsif, else, for, and while statments?
Code:
#!/usr/bin/perl -w
$number=int (rand 10);
print "Pick a number:";
$guess = <STDIN>;
chomp $guess;
while ($guess != $number){
if ($guess>$number){
print "You guess to high!\n";
print "Pick a number:";
$guess = <STDIN>;
chomp $guess;
}
elsif ($guess<$number){
print "You guess to low!\n";
print "Pick a number:";
$guess = <STDIN>;
chomp $guess;
}}
if ($guess== $number) {
print "You got it right!\n";
}
I just wanted to give a update. Still working on learning and I was proud to say I actually rewrote it with out looking at the book just using knowledge I learned and I think believe I am progressing but with practice I will get more advanced perl programs.
I just wanted to give a update. Still working on learning and I was proud to say I actually rewrote it with out looking at the book just using knowledge I learned and I think believe I am progressing but with practice I will get more advanced perl programs.
while($wrong < $guesses){ print "Pick a Number 1-10."; $guess=<STDIN>; if ($guess != $number){ $wrong++; $chancesleft=$guesses-$wrong; if ($guess < $number){ print "WRONG!\n"; print "You guessed to low! Please guess again!\n";
} if ($guess > $number) { print "WRONG!\n"; print "You guessed to high! Please guess again!\n";
} print "You have $chancesleft Chances Left!\n\n";
} else { print "Great Job! You Guessed it Right!\n"; exit; } } print "Thanks for Playing, Better luck next time!\n";
I know its still not much but I'm trying to get it down.
But thanks all for helping. Once I master this I plan to contribute to the Open source community.
By not using 'strict' you invite a lot of trouble - which otherwise would have been reported by the compiler. You also program using only global variables - which again is not a good idea.
Yes I am aware of them being global but I was not worried about it considered it is a small program and I don't plan to do further development. But with bigger programs I put variables in blocks. I was just mainly showing that I am writing a bit more cleaner and readable code. But still much room for improvement 1 step at a time. I am going to start using strict though so I does help me build cleaner programs.
The first computer program I ever wrote was eight lines long (in BASIC), took me six months to write, and had a bug in it.
I learned the craft by writing literally hundreds of programs. I was fascinated (and I still am ...) by how to make a machine do useful things. I'm also pleased to have the opportunity every day to spend time in the company of others who share my same fascination and addiction. May it never "wear off."
The beauty of computer programming is that there are no "rules." There are plenty of very good guidelines, and lots of war-story experience, but at the end of the day it is just you and the machine. It can go anywhere you can figure out how to take it, and can do anything you can figure out how to make it do. Plus, you can get paid for having fun! (If you don't go crazy first.)
In this program, global variables are adequate though.
I think I have already written it here - when one "studies" choreography, the tutors do not first teach to dance, they first teach choreographic positions, i.e. how to position hands, feet, shoulders - this is "static"; they teach movements - this is "dynamic", and only then they teach to dance - based on "static" and "dynamic".
My point is that getting rid of bad habits is much more difficult than net acquiring them in the first place.
I think I have already written it here - when one "studies" choreography, the tutors do not first teach to dance, they first teach choreographic positions, i.e. how to position hands, feet, shoulders - this is "static"; they teach movements - this is "dynamic", and only then they teach to dance - based on "static" and "dynamic".
My point is that getting rid of bad habits is much more difficult than net acquiring them in the first place.
Just curious, could you re-write this script without any global variable ?
Ok, you can
But what would be the advantage of making the data privates in this program ?
I mean, why overcomplicate things when acheiving simple tasks ?
I can see the advantage in larger program where you want to keep each process separate, but here there is not much to do
Ok, you can
But what would be the advantage of making the data privates in this program ?
I mean, why overcomplicate things when acheiving simple tasks ?
I can see the advantage in larger program where you want to keep each process separate, but here there is not much to do
As I said, the goal is having the right habits. The amount of global variables should be kept to bear minimum.
Encapsulation is a right habit - not having global variables is the first step on the path to encapsulation.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.