Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to read a string input from the user.
Go
var name string fmt.Print("Enter your name: ") fmt.Scanln([1]) fmt.Println("Hello,", name)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Passing the variable without & causes a runtime error.
Using *name is invalid because name is not a pointer.
โ Incorrect
In Go, to read input into a variable, you must pass its address using &.
2fill in blank
mediumComplete the code to read an integer input from the user.
Go
var age int fmt.Print("Enter your age: ") fmt.Scanln([1]) fmt.Println("You are", age, "years old")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Passing age without & causes input not to be stored.
Using *age is invalid since age is not a pointer.
โ Incorrect
Use &age to pass the address of the integer variable to fmt.Scanln.
3fill in blank
hardFix the error in reading a float input from the user.
Go
var price float64 fmt.Print("Enter price: ") fmt.Scanln([1]) fmt.Printf("Price: %.2f\n", price)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Passing price without & causes the value not to be stored.
Using *price is invalid because price is not a pointer.
โ Incorrect
To read input into a float64 variable, pass its address with &.
4fill in blank
hardFill both blanks to read two inputs: a string and an integer.
Go
var name string var age int fmt.Print("Enter name and age: ") fmt.Scanln([1], [2]) fmt.Println(name, age)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Passing variables without & causes input not to be stored.
Mixing & and non-& variables causes errors.
โ Incorrect
Use &name and &age to pass addresses of variables to fmt.Scanln for input.
5fill in blank
hardFill all three blanks to read a string, an integer, and a float input.
Go
var name string var age int var height float64 fmt.Print("Enter name, age, and height: ") fmt.Scanln([1], [2], [3]) fmt.Println(name, age, height)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Forgetting & causes input not to be stored.
Passing a variable without & among others causes runtime errors.
โ Incorrect
Pass the addresses of all variables using & to read multiple inputs with fmt.Scanln.