Presentation is loading. Please wait.

Presentation is loading. Please wait.

Asking the USER for values to use in a software 1 Input.

Similar presentations


Presentation on theme: "Asking the USER for values to use in a software 1 Input."— Presentation transcript:

1 Asking the USER for values to use in a software 1 Input

2 Asking the USER for values to use in a software 2 Input CAUTION: We use the word “input” in two different ways – be sure you understand how it’s being used.

3 Data Types A “data type” is: An “information category” – information that looks and acts similarly. Common data types (more will come later): Integers whole numbers Floats {floating point} numbers with a fractional part Strings {char}sequence of characters 3

4 “Solve for the area of a triangle” 4 Area Triangle solver

5 Inputs are hardcoded 5 Area Triangle solver screen area No external interface on the input side means that the values of base and height are hard-coded in the script. (They are not inputs) base height %givens base = 30.2; height = 20.3; %result area = 1/2*base*height;

6 User Interface 6 Area Triangle solver screen base height area keyboard External interface on the input side means that the values of base and height is entered by the user on the keyboard. (No longer hardcoded) Use of input: Here we use the word “input” to mean “information coming into the solver from outside” – input doesn’t always come from the user.

7 Keyboard interface There are built-in functions to get information from the user. 7

8 Keyboard interface There are built-in functions to get information from the user. These functions typically ask the programmer to supply prompts to clue the user on what is being asked. input() general form variableName = input(prompt); 8 Use of input: Here we use the word “input” to mean a specific function that collections information from the user.

9 Keyboard interface There are built-in functions to get information from the user. These functions typically ask the programmer to supply prompts to clue the user on what is being asked. input() general form variableName = input(prompt); Two examples given in the MATLAB help: num_apples = input('How many apples? '); name = input('Enter your name: ', 's'); 9

10 Syntax is data type dependent What data type is the user asked for? num_apples = input('How many WHOLE apples? '); The user is asked to provide a number – specifically an integer. 10

11 Syntax is data type dependent What data type is the user asked for? num_apples = input('How many WHOLE apples? '); The user is asked to provide a number – here, an integer. name = input('Enter your name: ', 's'); The user is asked to provide a sequence of characters – i.e. a string. 11

12 Syntax is data type dependent What data type is the user asked for? num_apples = input('How many WHOLE apples? '); The user is asked to provide a number – here, an integer. name = input('Enter your name: ', 's'); The user is asked to provide a sequence of characters – i.e. a string. These are the only 2 forms of using the input() built-in function. 12

13 Form #1. input() In the first form, only the prompt is inside the parentheses: num_apples = input('How many WHOLE apples? '); There is only one argument to the function. Arguments are inputs to the function – information being sent into the function so it can do its job. The one argument is the prompt string: 'How many WHOLE apples:' 13

14 Form #2. input(…, 's') In the second form of the input() function, there are TWO arguments: the prompt string, and another string: 's' name = input('Enter your name: ', 's'); 1 st argument 14

15 Form #2. input(…, 's') In the second form of the input() function, there are TWO arguments: the prompt string, and another string: 's' name = input('Enter your name: ', 's'); 1 st argument 2nd argument For this function, the second argument tells the input() function to expect a string from the user. 15

16 Form #2. input(…, 's') In the second form of the input() function, there are TWO arguments: the prompt string, and another string: ‘s’ name = input('Enter your name: ', 's'); If this argument is present, it must be the letter 's' and no other letter! 1 st argument 2nd argument 16

17 Code for the triangle solver Our program requires two values: a base and a height. To collect them from the user: base_cm = input('What is the base in cm? '); height_cm = input('What is the height in cm? '); 17

18 Code for the triangle solver The program requires two values: a base and a height. To collect them from the user: base_cm = input('What is the base in cm? '); height_cm = input('What is the height in cm? '); 18 Notice the space… why?

19 Code for the triangle solver The program requires two values: a base and a height. To collect them from the user: base_cm = input('What is the base in cm? '); height_cm = input('What is the height in cm? '); Without the space it’s not very pretty, and the user may actually press the space bar, which is a problem for strings: What is the base in cm?3.2 What is the height in cm?4 19 Notice the space… why?

20 % Collect inputs from the user base_cm = input('What is the base in cm? '); height_cm = input('What is the height in cm? '); % Compute the area of the triangle % Display the answer on the screen 20

21 % Collect inputs from the user base_cm = input('What is the base in cm? '); height_cm = input('What is the height in cm? '); % Compute the area of the triangle area_cm2 = 0.5 * base_cm * height_cm; % Display the answer on the screen % ??? How is the output displayed? 21

22 Wrapping Up The built-in function to prompt (i.e. ask) the user for a value (integer, float, string) is input() varName = input(prompt); 22

23 Wrapping Up The built-in function to prompt (i.e. ask) the user for a value (integer, float, string) is input() varName = input(prompt); There are 2 ways to use this function Just the prompt argument  obtain a numerical value With an additional argument ' s ' placed after the prompt argument, and separated by a comma  to obtain a string value 23

24 Wrapping Up The built-in function to prompt (i.e. ask) the user for a value (integer, float, string) is input() varName = input(prompt); There are 2 ways to use this function Just the prompt argument  obtain a numerical value With an additional argument ' s ' placed after the prompt argument, and separated by a comma  to obtain a string value Note: there is no method to get two values in one shot, write as many input() calls as needed. There are other ways to ask for values which we will cover later… 24

25 Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output 25

26 % Collect inputs from the user base_cm = input(‘What is the base in cm? ’); height_cm = input(‘What is the height in cm? ’); % Compute the area of the triangle area_cm2 = 0.5 * base_cm * height_cm; % Display the answer on the screen % ??? How is the output displayed? 26

27 Display with a specific format There are multiple ways to display the value of a variable 1. Use the semicolon appropriately 2. use the disp() built-in function 3. use the fprintf() built-in function 27

28 Display with a specific format There are multiple ways to display the value of a variable 1. Use the semicolon appropriately 2. use the disp() built-in function 3. use the fprintf() built-in function Each is used for specific reasons 1. Debugging – finding problems with the code 28

29 Display with a specific format There are multiple ways to display the value of a variable 1. Use the semicolon appropriately 2. use the disp() built-in function 3. use the fprintf() built-in function Each is used for specific reasons 1. Debugging – finding problems with the code 2. Simple programs, simple results (the programmer’s use) 29

30 Display with a specific format There are multiple ways to display the value of a variable 1. Use the semicolon appropriately 2. use the disp() built-in function 3. use the fprintf() built-in function Each is used for specific reasons 1. Debugging – finding problems with the code 2. Simple programs, simple results (the programmer’s use) 3. Formatted (“pretty”) output 30

31 In a short example, what are the differences? 31

32 % Collect inputs from the user base_cm = input(‘What is the base in cm? ’); height_cm = input(‘What is the height in cm? ’); % Compute/DISPLAY the area of the triangle area_cm2 = 0.5 * base_cm * height_cm 32

33 % Collect inputs from the user base_cm = input(‘What is the base in cm? ’); height_cm = input(‘What is the height in cm? ’); % Compute/DISPLAY the area of the triangle area_cm2 = 0.5 * base_cm * height_cm Not very pretty (nothing indicates where and what the output is) What is the base in cm? 3.2 What is the height in cm? 4 area_cm2 = 6.4000 The number of decimal places cannot be controlled, and it generally defaults to 4 in MATLAB. 33

34 % Collect inputs from the user base_cm = input(‘What is the base in cm? ’); height_cm = input(‘What is the height in cm? ’); % Compute the area of the triangle area_cm2 = 0.5 * base_cm * height_cm; % Display the answer on the screen % ??? How is the output displayed? disp(area_cm2); 34

35 % Collect inputs from the user base_cm = input(‘What is the base in cm? ’); height_cm = input(‘What is the height in cm? ’); % Compute the area of the triangle area_cm2 = 0.5 * base_cm * height_cm; % Display the answer on the screen % ??? How is the output displayed? disp(area_cm2); Not very pretty (nothing indicates where and what the output is) What is the base in cm? 3.2 What is the height in cm? 4 6.4000 35

36 % Collect inputs from the user base_cm = input(‘What is the base in cm? ’); height_cm = input(‘What is the height in cm? ’); % Compute the area of the triangle area_cm2 = 0.5 * base_cm * height_cm; % Display the answer on the screen % ??? How is the output displayed? disp(area_cm2); Not very pretty (nothing indicates where and what the output is) What is the base in cm? 3.2 What is the height in cm? 4 6.4000 The number of decimal places cannot be controlled using disp() either, and it defaults to 4 as well. 36

37 Using fprintf() 1. fprintf(...) % is the built-in function 37

38 Using fprintf() 1. fprintf(...) % is the built-in function 2.fprintf('format String InSeRtEd hErE! ') % The format string allows you to put words and specify a format (UPPER CASE vs. lower case, and punctuation only) 38

39 Using fprintf() 1. fprintf(...) % is the built-in function 2.fprintf('format String InSeRtEd hErE!') % The format string allows you to put words and specify a format (UPPER CASE vs. lower case, and punctuation only) 3. fprintf('format string with placeholders', list of variables to print are inserted here. If more than one variable is to be printed, each is separated by a comma from the previous one ) % Placeholders allow a specific format to be set (aligned right, and 2 decimal places for example) 39

40 Placeholders – why and how Placeholders are codes used in a format string which let the programmer use values stored in variables (without knowing the actual value) Why are placeholders needed? Suppose a variable, result, holds a value. Let’s further suppose it holds a float. How does the program print the value? Remember – the programmer may not know what value is in the variable. It may be computed during the running of the program. 40

41 Do not print a value Can this be the solution? fprintf('The value in result is 23.4\n'); 41

42 Do not print a value Can this be the solution? fprintf('The value in result is 23.4\n'); Result: >> fprintf('The value in result is 23.4\n'); The value in result is 23.4 >> 42

43 Do not print the variable name Can we just say this? fprintf('The value in result is: result\n'); 43

44 Do not print the variable name Can we just say this? fprintf('The value in result is: result\n'); Result: >> fprintf('The value in result is: result\n'); The value in result is: result >> 44

45 Do not forget the placeholder How about this? fprintf('The value in result is: ', result); 45

46 Do not forget the placeholder How about this? fprintf('The value in result is: ', result); Nope: >> fprintf('The value in result is: ', result); The value in result is: >> 46

47 Using placeholders The fprintf() function should print the value stored in the variable result. Placeholders to the rescue! fprintf('The value in result is %f meters.', result); “placeholder” (part of format string) 47

48 Using placeholders The fprintf() function should print the value stored in the variable result. Placeholders to the rescue! Result: >> fprintf('The value in result is %f meters.', result); The value in result is 33.651243 meters.>> 48 fprintf('The value in result is %f meters.', result); “placeholder” (part of format string)

49 Using placeholders The fprintf() function should print the value stored in the variable result. Placeholders to the rescue! Result: >> fprintf(‘The value in result is %f meters.’, result); The value in result is 33.651243 meters.>> 49 fprintf(‘The value in result is %f meters.’, result); DIFFERENT THAN ; OR DISP… 6 decimal places by default.

50 Stop for vocabulary Just a quick recall about the vocabulary. fprintf('The value in result is %f meters.', result); “function call” ‘format string’ “placeholder” (part of format string) variable to be printed 50

51 Most common placeholders Each data type has a placeholder Integer %d Floats %f Strings %s A single letter %c 51

52 Printing multiple variables When more than one variable must be printed to the screen, match each variable with its placeholder, and place the list of variables in order of the placeholders. Example age = input('Your age? '); %ask for age name = input('Your name? ', 's'); %ask for name fprintf('%s is %d years old. ', name, age); %display Sample run: Your age? 47 Your name? Fred Fred is 47 years old.>> 52

53 Special Characters Escape sequences can also be used within the format string: \n - this will create a new line when printing the string \t - tab (tabs the text to the right) '' - this will place one apostrophe in the final sentence displayed Example of all three: >> fprintf('%s''s age:\t\t%d years old\n\n', name, age); Fred's age:47 years old >> 53

54 Format Modifiers (1/4) Once the base placeholder is ready, modifiers further change how the values are displayed. Complete Format Modifier form: %-7.2f Left-justify the value TOTAL width to occupy Number of decimal places 54

55 Format Modifiers (2/4) To display a floating point value to 3 decimal places: fprintf('The value of pi: %-7.3f.', pi); Output: The value of pi: 3. 1 4 2 _ _ The value of pi: 3. 1 4 2. Underscores indicate whitespace – they will not actually show in the output. There are 7 spaces occupied 55

56 Format Modifiers (3/4) When debugging, it can be helpful to “delimit” the output (using dashes and > < symbols) – this lets you see where the “white space” is: fprintf('The value is:\t-->%9.3f<--\n\n', pi); Output: The value is:--> 3.142<-- >> The delimiters make it easy to notice the white space: spaces, tabs, newlines 56

57 Format Modifiers (4/4) Normally we don’t use the decimal place portion of format modifiers for strings, and it doesn’t work at all for integers – but the other portions still work! Example name = ‘Fred’; age = 47; fprintf(‘%-6s is %4d years old!\n’, name, age); Output: Fred is 47 years old! Note the spaces 57

58 Wrapping Up Display strings to the screen to: Give an introduction/welcome screen to the software Give error messages when invalid inputs Terminate the program nicely 58

59 Wrapping Up Display strings to the screen to: Give an introduction/welcome screen to the software Give error messages when invalid inputs Terminate the program nicely And of course… To display results Omit the semicolon (debugging purposes) Use disp() – debugging purposes as well Use fprintf() – specify a very specific format to display from 0 to an unlimited amount of variables 59

60 Wrapping Up Display strings to the screen to: Give an introduction/welcome screen to the software Give error messages when invalid inputs Terminate the program nicely And of course… To display results Omit the semicolon (debugging purposes) Use disp() – debugging purposes as well Use fprintf() – specify a very specific format to display from 0 to an unlimited amount of variables fprintf(…) requires placeholders, with or without any format modifiers: %d, %f, %s, %c, %-10.2f, %-5s, %2d 60


Download ppt "Asking the USER for values to use in a software 1 Input."

Similar presentations


Ads by Google