#!/usr/bin/perl # mathq version 1.0 use warnings; use strict; #<>= print "Welcome to the math program\n"; my $quit = 0; until ($quit) { #<>= my $first_number = int(rand(10)); # random integer from 0 to 9 my $second_number = int(rand(10)); # random integer from 0 to 9 # choose random operator code: 0 is division, 1 is multiplication my $operator = int(rand(2)); # random integer from 0 to 1 my $solution my $question; if ($operator == 1) { #<>= $solution = $first_number * $second_number; $question = "$first_number x $second_number = ?"; } else { #<>= $solution = $first_number * $second_number; # swap values of solution and first_number ($solution, $first_number) = ($first_number, $solution); $question = "$first_number / $second_number = ?"; } #<>= my $response; my $is_valid = 0; until ($is_valid) { print "$question\n"; $response = ; chomp($response); #<>= if ($response eq 'q' or $response =~ m/^\d+$/) { $is_valid = 1; } else { print "Invalid input: enter an integer or 'q' to quit\n"; } } #<>= if ($response eq 'q') { $quit = 1; # set loop control to indicate the user has quit } elsif ($response = $solution) { print "Correct\n"; } else { print "Incorrect: $question $solution\n"; } } print "exiting the math program\n";