Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7770991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:32:24+00:00 2026-06-01T16:32:24+00:00

I am trying to create a program where a user enters four numbers using

  • 0

I am trying to create a program where a user enters four numbers using regular expressions . If one of those numbers is 13 then the numbers to the left do not count toward the sum. My problem is creating an exception where none of the numbers equal 13. I cant seem to find a regular expression for my exception

puts "enter a number then hit enter four times"

number1 = STDIN.gets

number2 = STDIN.gets
number3 = STDIN.gets

number4 = STDIN.gets


if number1 =~ /13/ then
  puts number2.to_i + number3.to_i + number4.to_i
end

if number2 =~/13/ then
  puts number3.to_i + number4.to_i
end

if number3 =~/13/ then
  puts number4.to_i
 if number4 =~/13/ then
   puts "0"
 end
 if number1 != 13 or number2 != 13 or number3 != 13 or number4 != 13
   puts number1.to_i + number2.to_i + number3.to_i + number4.to_i
 end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:32:25+00:00Added an answer on June 1, 2026 at 4:32 pm

    If you want to use regular expressions, it should be mentioned that the following logic will yield true for more than just 13. It will also match 413, 131, 941771341, …

    if number1 =~ /13/ then
    

    Changing it to if number =~ /^13$/ then would be more accurate, but not as good as using to to_i.

    The other line in question…

    if number1 != 13 or number2 != 13 or number3 != 13 or number4 != 13
    

    …doesn’t work as expected because you are comparing a string to a number, and the logic join should be and. Comparing it to “13” won’t work either, since it is actually “13\n”. You can use number1.to_i != 13 and number2.to_i != 13 or something like number1 !~ /^13$/ and number2 !~ /^13$/ ... You could also figure out where to use an else statement in there.

    I really recommend studying the other answers though. They are far more elegant and Rubyesque.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a JavaScript program using the innerHTML function wherein the user
I am trying to create a program where that only one user can be
I am trying to create a helloworld program using only masm and not masm32
I am trying to create a program that has one tableview and when you
I'm trying to create a python program (using pyUNO ) to make some changes
I'm trying to create a program that: asks the user to input a max.
Ok, so I'm trying to create a program using a while loop to find
I am trying to create a program that the user must run the .exe
I'm trying to create a C# program using a Sharepoint Web Service reference that
I'm trying to create an android program. The program works like this, a user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.