ask a user their name, age, favorite color, and if they like dogs in swift

To ask a user for their name, age, favorite color, and if they like dogs in Swift, you can use the following code:

main.swift
// Ask for user's name
print("What is your name?")
let name = readLine() // read user input

// Ask for user's age
print("What is your age?")
let age = readLine()

// Ask for user's favorite color
print("What is your favorite color?")
let color = readLine()

// Ask if user likes dogs
print("Do you like dogs? (yes or no)")
let likesDogs = readLine()

// Print out user's information
print("Your name is \(name!). You are \(age!) years old. Your favorite color is \(color!). You \(likesDogs!) dogs.")
501 chars
19 lines

The readLine() function allows you to read user input from the console. Note that the user's input is stored as a String type, so you may need to convert it to a different data type if necessary.

gistlibby LogSnag