• Home
  • About
    • Chaeeun Gim photo

      Chaeeun Gim

      Game Development.

    • Learn More
    • Email
    • Twitter
    • Facebook
    • Instagram
    • Github
    • Steam
  • Posts
    • All Posts
    • All Tags
  • Projects
  • Concept Arts

Using Functions

14 Mar 2019

Reading time ~1 minute

Hello User

Instruction

■ ask the user to enter their name, age, and the current year,
■ calculate the year the user was born, and
■ output a greeting message.

require 'date'
require './input_functions'

INCHES = 0.393701  # This is a global constant

#Insert into the following your hello_user code
#from task 1.3P and modify it to use the functions
#in my_functions

def printAnswer(displayPrompt,prompt)
   prompt = prompt + "!"
   puts displayPrompt + prompt
end

def main
   name = read_string( "Please enter your name: ")
   displayPrompt = printAnswer("Your name is: ",name)
   name = read_string("what is your family name:  ")
   displayPrompt = printAnswer("Your family name is: ",name)

   userAge = read_string("What year were you born?")
   userAge = Date.today.year - userAge.to_i
   puts "you are " + userAge.to_s + " years old"
 
   userHeight = read_string("Enter your height in cms (i.e as a float): ")
   userHeight = userHeight.to_f * INCHES
   puts 'Your height in inches is: ' + userHeight.to_s

   read_boolean("Do you want to continue?")

end

main

input function

Hello User code call the function of 'input_function_code'▼
The codes are:

def read_string prompt
	puts prompt
   value = gets.chomp
end

def read_boolean prompt
    #puts prompt
    value = read_string(prompt)
    case value
	when 'y', 'yes', 'Yes', 'YES'
		puts 'Ok, lets continue'
	else
		puts 'ok, goodbye'
	end
end



codefunction Share Tweet +1