Task One Solutions

From PZwiki
Jump to: navigation, search
Language policy Language: [[::Task One Solutions|English]]


1

Swap the following variables around without using multiple assignment:

a = "A"
b = "B"


Solution:

a = "A"
b = "B"
temp = a
a = b
b = temp
print(a, b)

This requires you to use a third variable to hold the contents of one of the original variables. This is avoided when using multiple assignment.


2

This code contains errors, identify and fix them:

Hello = a 
b = Print
c = type
b(c(a))


Solution:

a = "Hello"
b = print
c = type
b(c(a))

When assigning a variable, the contents need to appear on the right of the equal sign, and the variable name to the left. Strings also require quotation marks " " or ' '. Remember that Lua is case sensitive, meaning Print is not a real function, unlike print. While variables that contain just a function is ugly (and fairly pointless), it does highlight that any piece of code can be held in a variable.

3

Without running it, what would the outcome of this code be?

a = type(type(1))
print(a)


Solution:

The type function always returns a string as it's result, regardless of the argument's datatype. Therefore, type of type is a string.