Question

In: Computer Science

MUST USE LOOPS. Write a perl program that replicates the actions of a pez dispenser. The...

MUST USE LOOPS. Write a perl program that replicates the actions of a pez dispenser. The program should print out the contents of the dispenser showing that it is empty, prompt a user for 10 pez candy flavors/colors, print out the contents of the dispenser showing that it is full, dispense the candies one at a time, and then print out the contents of the dispenser showing that it is empty. The dispenser should only take in 10 candies and should load and dispense the candies in opposite orders like a pez dispenser.

Solutions

Expert Solution

The program is given below.

#!/usr/bin/perl

my $choice = 1;
my @pez;
my @count = (1..10);
my $cand;
while($choice>0){
    print "1.Take a Candy\n";
    print "2.View Candies\n";
    print "Enter 0 to quit program \n";
    $choice = <>;
    if($choice == 1){
        $num = @pez;
        if($num == 0){
            print "Empty pez, Add 10 candies\n";
            for(@count){
                print "Enter candy:";
                $cand = <>;
                chomp( $cand );
                push(@pez,$cand);
            }
            print "Pez Dispenser full\n";
            print "Pez Dispenser Contents:\n";
            print "@pez\n";
          
        }
        else{
        $candy = pop(@pez);
        print "Popped candy: $candy\n";
        print "Remaining candies in pez dispenser are:@pez\n";
        }
    }

    elsif($choice == 2){
        print "The candies in pez dispenser are:\n";
        print "@pez\n";
      
    }
}

Screenshot of Output

                                          

First when you try to take a candy (option 1), it will promt youn to add 10 candies because the dispenser empty first. After that you can take one candy at a time and view the dispenser as needed. Enter 0 to quit the program.


Related Solutions

Write a perl program that replicates the actions of a pez dispenser. The program should print...
Write a perl program that replicates the actions of a pez dispenser. The program should print out the contents of the dispenser showing that it is empty, prompt a user for 10 pez candy flavors/colors, print out the contents of the dispenser showing that it is full, dispense the candies one at a time, and then print out the contents of the dispenser showing that it is empty. The dispenser should only take in 10 candies and should load and...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above...
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
Write a well-commented java program that demonstrates the use of loops, and generation of random integers...
Write a well-commented java program that demonstrates the use of loops, and generation of random integers (Code will be written in Dr. Java) You are taking some time off from your paint business and currently are on vacation in Bahamas. You decide to write a Java program that generates 10 random numbers between 1 and 20 (all integers). You cannot use arrays (even if you know what they are) to store these numbers. It then picks up the largest number...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In 2020 the average cost of living/month (excluding housing) for a family of 4 in Pittsburgh was $3850 per month. Write a program to print the first year in which the cost of living/month is over $4450 given that it will rise at a rate of 2.1% per year. (Note:  this program requires no input). Program 2: discount A discount store is having a sale where...
Using Perl language: Write a program that reads a string from the standard input, and uses...
Using Perl language: Write a program that reads a string from the standard input, and uses a Perl regular expression to test whether the string looks like a valid credit card number. The card numbers of four major companies follow the following formats: Visa 13 or 16 digits, starting with 4. MasterCard 16 digits, starting with 51 through 55. Discover 16 digits, starting with 6011 or 65. American Express 15 digits, starting with 34 or 37. You should test your...
Use "do...while" loops to write a program that finds all prime numbers less than a specified...
Use "do...while" loops to write a program that finds all prime numbers less than a specified value.
Write a program that uses nested for loops to print a multiplication table. Your program should...
Write a program that uses nested for loops to print a multiplication table. Your program should print out the multiplication table starting a 1 and going to 12. Output should be like: 1 x 1 = .1 x 2 = 2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT