| | |
perl assignment question
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 11
Reputation:
Solved Threads: 0
Im taking a perl class not exactly sure if what Im doing is right.
I was to take a written program that allowed the user to enter a number guessing what the target was. The original used elseif statements to let the user know if the guess was to high, too low, or correct. I need to make it so that it loops until the correct answer is given. Here is what I have so far. Any suggestions would be great.
I was to take a written program that allowed the user to enter a number guessing what the target was. The original used elseif statements to let the user know if the guess was to high, too low, or correct. I need to make it so that it loops until the correct answer is given. Here is what I have so far. Any suggestions would be great.
Perl Syntax (Toggle Plain Text)
#!/usr/bin/perl -w # guessnum2.pl use strict; my $target = 12; print "Guess my number!\n"; print "Enter your guess: "; my $guess = <STDIN>; if ($target == $guess) { print "That's it!! You guessed correctly:\n"; while (<STDIN> > $target) { print "Your guess is too high!\n"; } while (<STDIN> < $target) { print "Your guess is too low!\n"; }
A students best cheat tool is a search engine or the search feature of a website or forum. You would have found a recent thread discussing the exact same assignment had you done just a little searching.
http://www.daniweb.com/forums/thread169597.html
http://www.daniweb.com/forums/thread169597.html
•
•
Join Date: Dec 2008
Posts: 11
Reputation:
Solved Threads: 0
Thanks, i went back and looked at the other thread. This is what I got out of it to make it work for what I needed. Does this make sense?
Perl Syntax (Toggle Plain Text)
#!/usr/bin/perl -w # guessnum2.pl use strict; use warnings; my $target = 12; while { print "Pick a number\n"; my $guess = <STDIN>; chomp $guess; if ($guess > $target){ print "You guessed too high\n"; next; } elsif ($guess < $target) { print "You guessed too low\n"; next; } print "YOU GUESSED RIGHT!! It was $guess\n"; last; }
![]() |
Similar Threads
- PERL Text Statistic Help (Perl)
- The Pascal Triangle (Perl)
- Help with a Profile Managment System (Perl)
- HELP! What does this mean.......? (Visual Basic 4 / 5 / 6)
Other Threads in the Perl Forum
- Previous Thread: Elapsed Time for Application
- Next Thread: Running perl programs
| Thread Tools | Search this Thread |






