Uipath Session 2 - Lesson 2 - Practice 1 To 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

UiPath Academy

RPA Developer Basic Training


Lesson 2 - Variables, Data Types & Control Flows
Practice
Table of Contents

Lesson 2 - Variables, Data Types & Control Flows


1. Simple Flowchart Control - Open Notepad
2. Guessing Game - 2 flavors
3. Iterating an Array of Fruits
4. Stoplight Color Switch Exercise
5. Generic Value and data types

Lesson 2 - Variables, Data Types & Control Flows


Lesson 2 - Practice 1 Outline

Create a flowchart which performs the following actions:

● Starts by asking the user a mathematical question (e.g. the equation, 1 + 1 = 2) in a Message Box with
Yes/No choices and capturing the user’s answer:

A. If the user answered Yes (which is the True answer):


o Opens Notepad.exe
o Attaches to the window of Notepad.exe
o Upon attaching to the window, types Correct into it.
B. If the user chose No (which is the False answer):
o Does the same series of actions as the True branch. However, instead of typing Correct, type
Incorrect into Notepad.

Page 2 of
● Finally, closes the Notepad application.

Lesson 2 - Practice 1 Walkthrough

Create a new Flowchart and call it Simple flow Decision.

Drag a Message Box activity below the start node. Fill in the Message property field with the mathematical
question (e.g. 1 + 1 = 2 ?).

● Set the Buttons property to YesNo.


● Create an output variable called answer to capture the user’s choice
Find a Flow Decision activity in the Activities Panel and drag it below the Message Box. We are going to make
our decision based on the user’s answer.

Next, we are going to use a Sequence within this Flowchart, so find and drag a Sequence from the Activities
Panel into the Main window and double click it to view its contents.

Page 3 of
Once opened, find and drag an Open Application activity into the sequence. Now we must indicate the
application that is to be opened. There are two ways in which you can do this. The simplest way to do this is to
have Notepad opened on your desktop, click the Indicate window on screen button in the Open Application
activity, then click on the Notepad window.

An alternative way is to enter the full path of the executable file into the FileName parameter of the Open
Application activity, as shown below. The full path in this instance would be
C:\Windows\System32\notepad.exe (remember that all strings have to be placed between quotation
marks).

Page 4 of
Page 5 of
Next, find and drag a Type Into activity into the Do container of the Open Application activity. If there is a
specific area within the window where we want to type, we have to indicate the element again. However, in
this case, this is not necessary.

The next step is to add the text that is to be typed into Notepad. Within quotation marks, type Correct. Now
we have a complete sequence.

Page 6 of
Back in the main flowchart, copy and paste the previous sequence so it appears twice. Rename the sequences
to Correct Sequence and Incorrect Sequence. Open the Incorrect Sequence and change the text from Correct
to Incorrect (remember to write between quotation marks).

Finally, add a Close Application activity to close Notepad at the end of the flowchart. Again, find this activity in
the Activities Panel and drag it into your workflow, below the other activities. The application that the activity
must interact with has to be identified again, so we must follow the same steps as before. Connect both the
correct and incorrect sequences to this activity. Your finished workflow should look like this:

Page 7 of
Page 8 of
Run the workflow. You should see Notepad being opened and then Correct being typed in before the
application is closed again (you may need to click Don’t Save when the pop up appears during closing).

Lesson 2 - Practice 1 Answers

Please insert the automated workflow with the correct solution, as described above, so that the users can
upload directly in Studio.

Guessing Game Workflow


Simple Flowchart

Lesson 2 - Practice 2 Outline


Create a flowchart with the following properties:

● The robot picks a random number for us to guess, guided by simple hints.
● The robot should generate a random number and ask the user to guess it through an Input Dialog
activity.
● The robot will indicate if the generated number is larger or smaller than the user’s guess

A. Build the guessing interaction by using a flowchart diagram

B. Build the guessing game by using a sequential layout

Lesson 2 - Practice 2 Walkthrough


Guessing Game
Flowchart

Page 9 of
The first action that must be performed is creating a random number that users have to guess. Since a range
like 0-999 is perfect for this sort of game, we will create an integer variable which has a value that corresponds
with the current time in milliseconds..

● Create a variable of the Int32 type, either through the Create Variable button on the menu bar or the
Variables Panel.
○ Name the variable number.
○ After naming it, set the Type from Generic Value to Int32 in the variable type drop-down
menu.
● Set the value to DateTime.Now.Millisecond, without quotation marks.
○ This reads the current system time, and only takes the milliseconds value.

Now that there is a random number to guess, use an Input Dialog activity to prompt the user for a number.
Make the prompt dynamic, so it can change as the user guesses and reuses the guessing prompt.

● Find and insert an Input Dialog activity and drag it below the start node.

● Create a String variable called hint and give it a value similar to “Please pick a number between 1
and 999”.
○ This will be the default value. In other words, this will be the first prompt that the user will
see. When it changes, the original value will be overwritten with different prompts.
● In the Input Dialog activity, insert hint in the Label property field.
● In the Title field, enter something like “Guessing Game”.

Page 10 o
● Create another Int32 variable and name it guess, leaving the default value blank. Place this variable
name in the Output field of the Input Dialog activity.

● Here is what the variables panel should look like at this point:

This answer should lead to a decision node which evaluates if the guessed number is the correct number. If
correct, the process should display a message box notifying the user that the guess was correct. If incorrect,
the process should go to another decision node to evaluate whether the guess was too high.

● Search for and add a Flow Decision activity under the Input Dialog.
● In the Condition field, type number = guess to see if the variables have equal values.
● Search for and add a Message Box activity.
● In the Text field, write something like "Congratulations! You have guessed the number!”

Page 11 o
○ For those with previous programming experience, concatenation can be used to display the
number as well: "Congratulations! You have guessed the number "+number.ToString
+".
● Connect the True branch of the decision node to the Message Box activity.
● Search for and add a new decision node.
● Connect the False branch of the first decision node to the second decision node.

The second Flow Decision evaluates the user’s guess and displays whether the number was too high or too
low. If the guess was too high, the Assign will set the value of hint to “Try again lower.” If the guess was too
low , the Assign will set the value of hint to “Try again higher”.

● In the second decision node, insert ‘guess > number’ into the Condition field.
● Find the Assign activity and add two of them into the workflow.
○ One should assign the value “Try again lower”
○ The next should assign the value “Try again higher” to hint.
■ These activities dynamically change the prompt that the user will see.
● Connect the True branch of the decision node to the first Assign activity.

Page 12 o
● Connect the False branch of the decision node to the second Assign activity.

Make sure the prompt keeps reappearing until the user finds the correct answer.

● Connect both Assign activities to the Input Number activity


○ This creates a loop that repeats the actions until the right answer is chosen.

This is what the final workflow should look like:

Sequence

The first action that must be performed is creating a random number that users have to guess. Since a range
like 0-999 is perfect for this sort of game, we will create an integer variable which has a value that corresponds
with the current time in milliseconds.

Page 13 o
● Create a variable of the Int32 type, either through the Create Variable button on the menu bar or the
Variables Panel.
○ Name the variable number
○ After naming it, set the type from Generic Value to Int32 in the Variable Type drop-down
menu
● Set the value of number to DateTime.Now.Millisecond
○ This evaluates the current time and takes the value in milliseconds

Now that there is a random number to be guessed, create a new Sequence and use an Input Dialog activity to
prompt the user for a number. Make the prompt dynamic so it can change as the user guesses and reuses the
guessing prompt. This sequence will be inserted into a Do While loop later.

● Drag a Sequence into the Main panel.


● Drag an Input Dialog activity into the sequence.
● Create a String variable called hint and give it a value resembling “Please pick a number between 1
and 999”.
○ This will be the default value. In other words, this will be the first prompt that the user will
see. When it changes, the original value will be overwritten with different prompts
● In the Input Dialog activity, insert hint into the Label field.
● In the Title field, insert a title, such as “Guessing Game”.

Page 14 o
● Create another Int32 variable, name it guess and leave the default value blank. Place this variable in
the Output field of the Input Dialog activity.

Page 15 o
● The Variables panel should look like this:

The next activity in the sequence should be an If statement that evaluates the user’s guess and displays
whether the number was too high or too low. If the guess was too high, the Assign will set the value of hint
to “Try again lower”. If the guess was too low, the Assign will set the value of hint to “Try again higher”.

● Under the Input Dialog activity, find and add an If activity.


○ Insert ’guess > number’ into the Condition field.
● Drag two Assign activities and add them into each of the branches in the If activity.
○ The one on the Then branch should assign hint the value “Try again lower”.
○ The one on the Else branch should assign hint the value “Try again higher”.
■ These activities dynamically change the prompt that the user will see.

Page 16 o
● The sequence should look something like this:

To make this prompt keep appearing until the right number is guessed, add a loop that only ends when the
guess equals the number. Let the user know that their guess was right when the loop ends.

● Search for and add a Do While activity and place it right above the sequence added earlier, but still
inside the Main sequence.
● Click and drag the sequence added earlier into the body of the Do While activity.
● In the Condition field of the Do While activity, enter ‘guess <> number’.
○ This evaluates if the guess is not equal to the number. If they are not equal, the loop
continues.

Page 17 o
● Search for and add a Message Box activity below the Do While loop.
● In the Text field, display the text "Congratulations! You have guessed the number!”
○ For those with previous programming experience, concatenation can be used to display the
number as well: "Congratulations! You have guessed the number "+number.ToString +".".

This is what the final workflow should resemble:

Page 18 o
Page 19 o
Lesson 2 - Practice 2 Answers

Please insert the automated workflow with the correct solution as described above, so that the users can
upload it directly in Studio.

Download the .xaml solution for exercise 2 - flowchart file here.


Guessing game - flowchart - final

Download the .xaml solution for exercise 2 - sequence file here.


Guessing game - sequence - final

Lesson 2 - Practice 3 Outline


Iterating an Array of Fruits:
Create a sequence with the following properties:

● A variable called testList, of String Array type, with the value: {“apples”, “lemons”, “mangos”,
“apricots”, “pears”, “oranges”, “kiwis”, “bananas”, “lychees”, “plums”}.
● Iterate through each of the array values.
● When iterating, write lines saying “I like to eat “ before each array value in the Output Panel.
● When a value that starts with ‘k’ is reached, break the loop after writing the corresponding line.

Lesson 2 - Practice 3 Walkthrough

Drag and drop a sequence from the Activity Panel.

Create a variable called testList in the scope of the sequence. Set its type to String Array and fill in the
array with the given values.

Page 20 o
● Create a variable using the Create Variable button in the top toolbar (with a type of generic value), or
using the Create Variable line in the Variables Panel.
● Name it testList.
● Change the variable Type to an Array of [T], and select String. This makes it an array of string objects.

● In the default value column, enter or copy and paste the array values: {“apples”, “lemons”, “mangos”,
“apricots”, “pears”, “oranges”, “kiwifruit”, “bananas”, “lychees”, “plums”}.
● This is what the variable should look like:

Find and create a For Each activity. For the list of items to iterate through, enter the variable testList.
Make the For Each activity write a line saying “I like to eat” that is concatenated with the item value in each
loop iteration.

Drag and drop the For Each activity after searching for it in the Activity Panel.

● Enter the string array created earlier, called testList, in the Values field of the For Each activity.
● Search for the Write Line activity in the Activities Panel and drag it into the body of the For Each
activity.
● In the Text field of the Write Line activity, write “I like to eat ” & item.ToString.
○ Make sure to include a space before the second quotation mark.

Page 21 o
○ For those unfamiliar with the notation, the & symbol concatenates the string “I like to eat “
with the string version of the current item in testList.
● This is what the workflow should look like at this point:

Run the process and navigate to the Output Panel, which should look like this:

Page 22 o
Next, evaluate if any item in testList starts with the letter ‘k’ and if it does, then break the loop.

● Search for and add an If activity below the Write Line activity.
● In the Condition field, enter the expression “item.ToString.StartsWith(“k”)”.
○ This evaluates if the current item in the list that is being looped through starts with the
character “k”.
● Find and add a Break activity into the Then branch of the If activity.
● Leave the Else branch of the If activity empty.

Run the process again. This time, the output should show every line until ‘I like to eat kiwifruit.’ Your final
workflow should resemble this:

WORKFLOW:

Page 23 o
Page 24 o
Lesson 2 - Practice 3 Answers

Download the .xaml solution file for Exercise 3 here.


Iterating an array of fruits - final

Lesson 2 - Practice 4 Outline


Stoplight Color Switch Exercise

Create a modular stoplight process that:

● Has an input variable which has a value of either “red”, “green” or “yellow”.
● Could take the value from the user with a multi choice dialogue (optional).
● Has a separate logging workflow which writes what color the stoplight is and how long it will
stay on in the Output Panel.
○ The default color in the logging workflow arguments should be “red”, to be on the
safe side.
○ It will take in two arguments, color and duration.
● Has a main workflow that will invoke the logging workflow with different arguments
depending on the color, as follows:
○ Red - 50 seconds
○ Yellow - 10 seconds
○ Green - 80 seconds

Lesson 2 - Practice 4 Walkthrough

First, create the logging workflow. It should be able to take in arguments for color and duration of the
stoplight.

● Create a new sequence workflow.


● In the Arguments tab at the bottom (switch the bottom panel from Variables to Arguments), add in
an argument called color.

Page 25 o
○ Set the Direction to In and the Type to String.
○ Its default value should be red (required in the exercise text).
● Add another argument called duration.
○ Set the Direction to In and the Type to Int32.

It should also log the color and duration of the stoplight in the Output Panel.

● Find and add two Log Message activities into the main sequence.
○ If you used Write Line earlier, these are very similar. However, Log Message activities have
more versatility as they can write information lines as well as troubleshooting lines.
● For the expression of the first Log Message activity, type “The streetlights now show “ + color
○ Set the Level to Info.
● For the second Log Message, set the expression to “This color will now stay on for “ +
duration.ToString + “ seconds”.
○ Set the Level to Info.
● The completed logging workflow should resemble this:

Page 26 o
Onto the main workflow, create a new Flowchart workflow. Then, create the variable that will store the
stoplight color, as well as a Switch statement that evaluates the variable.

● Create a variable called stopLightsColor.


○ Its Type should be String.
○ You can set its value to any of the possible inputs (“red”, “yellow”, “green”). Go ahead and run
it 3 times setting the value to each of these and see the difference in the output.
○ We’ll set it with “red” as default, for safety.

● Let’s allow the user to choose the value (optional).


○ We add an Input Dialog activity.
○ In the Options property, we’ll set the user’s possible choices {"red", "yellow", "green"}.
○ We save the chosen value to the stopLightsColor variable by setting it in the Output
property.

● Find and create a Switch activity and connect it under the Start node.

Page 27 o
○ We chose this activity as it’s especially useful for branching the workflow when there are more
than 2 possible outcomes with equal weight. Adding chained simple decision activities (or
nested if blocks in a sequence) would not be a good practice in this case.
○ In the Expression field on the Properties Panel, enter the recently created variable,
stopLightsColor.
○ Make sure the TypeArgument selection is a String type.

Now, set three different cases for the Switch statement that invoke the logging workflow with different
stoplight values.

● Find and create three Invoke Workflow File activities.


● Double-click on the first one and set the WorkflowFileName to “log information.xaml”.
○ If the file path to the logging workflow is different, change the name accordingly.
● Click on the Import Arguments button to import the arguments from the logging workflow
automatically.
○ In the Argument window that appears, set the value of color to stopLightsColor.
○ Set the value of Duration to 50.
○ Name the activity Invoke logging with red.
● Do the same two steps above for the next two Invoke Workflow File activities.
○ For both, color should have a value of stopLightsColor.
○ For yellow, the duration should be 10, for red it should be 80.
● Connect the Switch node to each Invoke Workflow activity.
○ Click on the arrow leading to the red one and set the DefaultCaseDisplayName to “red”
without quotation marks.
○ For this arrow alone, select the box labeled IsDefaultCase to set it as the default case.

Page 28 o
● Set the case names for the other two colors.
● This is what the completed workflow should look like:

Page 29 o
Run the workflow and check the Output tab. The output should correspond to the color red.

● Change the color by modifying the default value of the stopLightsColor variable to see what
changes.

Lesson 2 - Practice 4 Answers

Download the .xaml main workflow for Exercise 4 here.


Stoplight color switch - main - final
Download the .xaml logging workflow for Exercise 4 here.
Stoplight color switch - logging - final

Lesson 2 - Practice 5 Outline


Generic Value and data types #tryityourself
Create four variables in your sequence

1. A with value “123”


2. B with value “456”
3. C with value 123
4. D with value 456

Print to the console various operations with them and see what is the result

● A + B
● C + D
● A + C

Page 30 o
● C + A

Lesson 2 - Practice 5 Walkthrough

Just create the variables and add a few Write line / Log Message / Message Box activities and try the
indicated operations.

Any surprises in the output?

Page 31 o
Lesson 2 - Practice 5 Answers

Download the .xaml main workflow for Exercise 5 here.


Generic value

Page 32 o

You might also like