| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 12409 | Python2020_MOOCS_Practice_Problem1 |
|
| 12710 | Python2020_MOOCS_Practice_Problem2 |
|
| 12713 | Python2020_MOOCS_Practice_Problem3 |
|
| 12860 | Python2020_MOOCS_Practice_Problem4 |
|
Description
Write a program that prompts the user to input two strings and reports the two strings' lengths, by reporting the shorter string first. But if they are of the same length then keep them in the original order. For example, (blue text = typed input, green highlight = program printout)
$ python3 compstr.py
Enter a string:
Great
Enter another string:
job
Shorter string: job (length 3)
Longer string: Great (length 5)
$ python3 compstr.py
Enter a string:
Mary
Enter another string:
lamb
First string: Mary (length 4)
Second string: lamb (length 4)
$
Note that in case the strings are of different lengths, the program says Shorter and Longer, but in case the strings are of equal length, the program says First and Second. You don't actually print in color… the letters are colored for illustration purpose only.
[HINT] Your program should start with these two lines:
s1 = input('Enter a string:\n')
s2 = input('Enter another string:\n')
Input
typed input (blue text)
Output
program printout (green highlight)
Sample Input Download
Sample Output Download
Discuss
Description
Write a Python program that takes two names from the inputs and prints two sentenses "XXX and OOO are my friends." and "OOO is my best friend" to the output, where "XXX" and "OOO" are the names from the inputs.
Assume the program is written in a file named "friends.py" and run from the command line. The following block shows how the terminal shall look like: (the text in pink is typed by the user; others are printed by the program)
$ python3 friends.py
Mary Cindy
Mary and Cindy are my friends.
Cindy is my best friend.
$
[Hint]
There is a space between "Mary" and "Cindy". Actually you might get "Mary Cindy" by calling input() just once. You should then use another method to separate the two names.
Watch out for spaces and new line characters -- any difference makes your code not accepted by Online Judge.
In this problem, one extra empty line is required after the both output sentence, "Mary and Cindy are my friends." and "Cindy is my best friend.".
Input
One sentence includes two names and one space between the names.
Output
Two sentences claiming your friends and your best friend with one extra empty line after each of the sentences.
Sample Input Download
Sample Output Download
Discuss
Description
This problem is the same as the previous one (Python2020_Self-Check4_Problem2), except for some minor changes in the output for you to understand some tricky things.
$ python3 friends.py
Mary Cindy
Mary and Cindy are my friends.
Cindy is my best friend.$
[Hint]
There is a space after "Mary and Cindy are my friends.". Select(反白) the text to see the hidden space. We won't be so tricky in the quizzes, but it's good for you to know what to be careful of.
There is no new line character after "Cindy is my best friend.". The dollar sign ($) is the prompt, not part of the program output.
You can refer to 7:47 in the 3rd lecture video of week 3 for how to omit the new line character when printing.
Please check out the description and sample output of the previous two problems and this one. What's the difference among "an new line character after the last line"(the usual case), "one extra new line after the last line", and "no new line character after the last line"?
If you are using Cygwin or other terminal, you'll see the differences among the three cases. If you are using Jupyter Notebook, you'll find out that the first case and the third case look like the same. Therefore, be very careful for new line characters if you want to use Jupyter Notebook for the quizzes.
Input
One sentence includes two names and one space between the names.
Output
Two sentences claiming your friends and your best friend with no new line character after the the last sentence.
Sample Input Download
Sample Output Download
Discuss
Description
Write a program to record the daily expense.
Use a data structure (e.g. a list of tuples) to keep the records of expense, date, and description.
Define 3 functions: write_record, list_records, and total_expense. write_record writes to the data structure, list_records prints out the records, and total_expense returns the total expense.
The function write_record should accept 3 parameters, representing expense, date, and description. Date is an optional parameter with default value being "2020/01/18". Description is also an optional parameter with default value of being "something".
The function list_records has no return value. It should print out all the records, one in a line, in the format as "$100, 2020/01/18, lunch".
The function total_expense returns the total expense, in the type of integer.
Example usage:
>>> write_record(40, '2020/01/16', 'breakfast')
>>> write_record(99, date='2020/01/17')
>>> write_record(300, description='book')
>>> write_record(55)
>>> list_records()
$40, 2020/01/16, breakfast
$99, 2020/01/17, something
$300, 2020/01/18, book
$55, 2020/01/18, something
>>> print(total_expense())
494
[NOTE]
For judging purpose, the code you submit should take 4 strings representing the function calls of write_record from input and execute the function calls using the built-in function exec.
Then call list_records and print the returned value from total_expense at the end.
In short, you should use the following template code and fill in the required parts.
def write_record(...): # YOUR PARAMETERS
# YOUR CODE to write to records
def list_records():
# YOUR CODE to print the records
def total_expense():
# YOUR CODE to return the total expense
for i in range(4):
calling_write_record = input()
exec(calling_write_record)
list_records()
print(total_expense())
Input
4 lines of string representing the function call of write_record.
Output
4 lines of record printed by list_records() and 1 line of the total expense.