I am attempting to get my current program to give the user three different input options and notify the user if they have attempted to use the same input option twice. My current code:
prompt = "Do you want to use the Subscription coupon[1] the one-time pay-per-use
coupon[2] or the gift purchase coupon[3]: "
print prompt
code = gets.chomp
code = code.to_i
history = []
loop do
if code == 1
puts "Subscription coupon used"
print prompt
code = gets.chomp
elsif code == 2
puts "One-time pay-per-use coupon used"
print prompt
code = gets.chomp
elsif code == 3
puts "Gift purchase coupon used"
print prompt
code = gets.chomp
else history.include? code
puts "Code already input"
print prompt
code = gets.chomp
end
end
It is allowing me to input as many times as I want, but no matter what the second input is it prints the code already input text.
How do I fix the code to get it to run in the intended way?
You might consider writing that as follows. Note that I've added a bit.
valid_entries = ["1", "2", "3", "4"]
answers = []
loop do
puts "\nDo you want to use the"
puts " Subscription coupon (1)"
puts " One-time pay-per-use coupon (2) or"
puts " Gift purchase coupon (3)?"
puts "Enter (4) if you are finished"
print "Your selection: "
entry = gets.chomp
unless valid_entries.include?(entry)
puts "'#{entry}'is not a valid entry, you dummy. Try again."
next
end
if answers.include?(entry)
puts "You've already made that selection. Try again."
next
else
answers << entry
end
break if entry == "4"
puts case(entry)
when '1' then "Subscription coupon used"
when '2' then "One-time pay-per-use coupon used"
when '3' then "Gift purchase coupon used"
end
end
Here is a possible conversation.
Do you want to use the
Subscription coupon (1)
One-time pay-per-use coupon (2) or
Gift purchase coupon (3)?
Enter (4) if you are finished
Your selection: 1
Subscription coupon used
Do you want to use the
Subscription coupon (1)
One-time pay-per-use coupon (2) or
Gift purchase coupon (3)?
Enter (4) if you are finished
Your selection: 2
One-time pay-per-use coupon used
Do you want to use the
Subscription coupon (1)
One-time pay-per-use coupon (2) or
Gift purchase coupon (3)?
Enter (4) if you are finished
Your selection: 1
You've already made that selection. Try again.
Do you want to use the
Subscription coupon (1)
One-time pay-per-use coupon (2) or
Gift purchase coupon (3)?
Enter (4) if you are finished
Your selection: %
'%'is not a valid entry, you dummy. Try again.
Do you want to use the
Subscription coupon (1)
One-time pay-per-use coupon (2) or
Gift purchase coupon (3)?
Enter (4) if you are finished
Your selection: 4
As an alternative to
puts case(entry)
when '1' then "Subscription coupon used"
when '2' then "One-time pay-per-use coupon used"
when '3' then "Gift purchase coupon used"
end
you could write
puts MESSAGES[entry]
after having defined the constant
MESSAGES = { '1'=>"Subscription coupon used",
'2'=>"One-time pay-per-use coupon used"
'3'=>"Gift purchase coupon used" }
Move the history check to the beginning of your loop, and actually populate the history. Here's one of many ways to accomplish that:
loop do
if history.include?(code)
puts 'Code already input'
print prompt
code = gets.chomp.to_i
next # Stop processing this iteration of the loop immediately, and skip to the next iteration.
else
history << code
end
if code == 1
puts 'Subscription coupon used'
elsif code == 2
puts 'One-time pay-per-use coupon used'
elsif code == 3
puts 'Gift purchase coupon used'
else
puts 'Invalid entry.'
end
print prompt
code = gets.chomp.to_i
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.