1. GOALS/OUTCOMES FOR LAB
- To use continue practice with variables and input
- To practice string processing
- To practice formatted output
- To get you more familiar with your PyCharm IDE
- To write your own Python code
2. LAB 2 – TASK
Task 1 [Simple exponent]:
- Prompt the user for input in the following form: “x^y”, where x and y are a single digit number, for example 2^8 or 4^2 would be valid inputs.
- Next, output the string “x^y is XXX”, where x and y are replaced with the input values and XXX is the result of raising x to the y power, e.g., xy.
Note that this syntax “x^y” is how some other programming languages perform exponent. Recall, Python uses the ** operator for exponent. Your prompt and output should look like that shown on page 3 of this document. You may assume that the input is correct. That is, you do not need to include any code to check to see if the second character is a “^”. We haven’t learned how to do that yet. Also, see accompanying video linked above.
See next page for Tasks 2-4
Task 2 [Converting a sentence first half to uppercase and second have to lowercase]
- Prompt the user to input a long sentence (you may assume the user types at least 3 or more characters).
- Remove any spaces that appear before or after the string.
- Print out the length of the string and the middle character as shown on page 3. Note: you should use integer divide to compute the middle character index.
- Finally, print out the string with the first half (up to, but not including the middle character) are all uppercase m, followed by a vertical bar symbol “|”, followed by second half of the sentence (from the middle character to the last character) all in lowercase.
Your output should look like those shown on page 3 of the lab document. Also see accompanying video.
Task 3 [Remove commas, periods, and convert all spaces to * and all letters to lowercase] – Prompt the user to input a long sentence.
- You should remove any spaces that appear before or after the string.
- Remove all commas and periods from the input.
- Replace all the spaces (” “) in the string with an asterisks *.
- All letters should also be converted to lowercase.
- Printout this processed string.
Your output should look like that shown on page 3 of the lab document. Also see accompanying video.
Note you do not have to perform the operations in the order specified above. Just as long as your output is the same as the required output.
Task 4 [Highlighting a substring]
- Prompt the user to input a sentence.
- Prompt the user to input a substring (i.e., a smaller sequence of characters that is part of the first sentence). You may assume this is a valid substring.
- Output the original string, but where the substring first occurs should be modified to be in all uppercase and be surrounded by asterisks *. See page 3 for an example.
Hint: The .replace() methods will note help for this task. In my opinion, this is the most challenging of the 4 tasks.
Your output should look like that shown on page 3 of the lab document. Also see accompanying video.
Example video for the lab: https://www.eecs.yorku.ca/~mbrown/EECS1015_Lab2.mp4
Lab 2 – Example output #1 (Key: green are prompts, red is user input, blue is output you generate)
——
Task 1: Type in an exponential in the following form x^y, where x and y are single digits from 0-9
Type exponent here: 2^4
2^4 is 16
——
Task 2: First half upper, second half lower
Type a sentence: This is a long SENTENCE.
The string is 24 chars long. ‘n’ is the middle character.
Modified sentence: THIS IS A LO|ng sentence.
——
Task 3: Remove commas and periods. Convert spaces to * and characters to lowercase.
Type a sentence: Hello, this is another long sentence.
Modified sentence: hello*this*is*another*long*sentence
——
Task 4: Substring highlight
Type a sentence: York University is in Toronto.
Type substring : Toronto
Modified sentence: York University is in *TORONTO*.
Lab 2 – Example output #2
——
Task 1: Type in an exponential in the following form x^y, where x and y are single digits from 0-9
Type exponent here: 8^3
8^3 is 512
——
Task 2: First half upper, second half lower
Type a sentence: Yet another long SENTENCE with mixed CASES.
The string is 43 chars long. ‘E’ is the middle character.
Modified sentence: YET ANOTHER LONG SENT|ence with mixed cases.
——
Task 3: Remove commas and periods. Convert spaces to * and characters to lowercase. Type a sentence: Strings, integers, and floats are basic DATA TYPES in Python.
Modified sentence: strings*integers*and*floats*are*basic*data*types*in*python
——
Task 4: Substring highlight
Type a sentence: The quick brown fox jumps over the lazy dog.
Type substring : brown
Modified sentence: The quick *BROWN* fox jumps over the lazy dog.
Task 0:
- File name must be “py” (all lowercase, no spaces)
- The Python comments at the beginning of your program must include your name, email, and York student id (this is important for grading)
- If your file name is incorrect, your or do not put in the required information we will deduct -5 points (Why are we so harsh? Because if you don’t put in your name and student id it can be very difficult for the TAs to determine whose submission this is.)
Task 1-4:
- Each task should prompt the user correctly and compute the required output correctly.
–
Reviews
There are no reviews yet.