Created
May 5, 2015 04:08
-
-
Save jmbuscemi/4b1c9a56ed865ea3c63f to your computer and use it in GitHub Desktop.
User input tracker (hardmode)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
continue = true | |
count = 0 | |
sum = 0 | |
puts "Give me some input (words or numbers): " | |
answer = gets.chomp | |
words = "" | |
#Concatenate words and exit once numbers are entered | |
if (answer.to_i.to_s != answer) && (answer.to_f.to_s != answer) #evaluates words | |
while continue | |
if answer == "" | |
puts "Goodbye!" | |
continue = false | |
elsif !((answer.to_i.to_s != answer) && (answer.to_f.to_s != answer)) | |
puts "Input needs to be a word! You must start over" | |
continue = false | |
else | |
words += answer | |
puts "Input more words (or Enter to EXIT): " | |
answer = gets.chomp | |
end | |
end | |
puts "Your string was #{words}" | |
#Calculate sum and average of numbers and exit once words are entered | |
elsif !((answer.to_i.to_s != answer) && (answer.to_f.to_s != answer)) | |
while continue | |
if answer == "" | |
puts "Goodbye!" | |
continue = false | |
elsif (answer.to_i.to_s != answer) && (answer.to_f.to_s != answer) | |
puts "Input needs to be a number! You must start over" | |
continue = false | |
else | |
sum += answer.to_f | |
count +=1 | |
puts "Input more numbers (or Enter to EXIT): " | |
answer = gets.chomp | |
end | |
end | |
average = sum / count | |
puts "The sum of your numbers was #{sum} and the average was #{average}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment