Our parents told us that we must eat vegetables to be healthy. And it turns out, our parents were right! So, what else did our parents tell?
Our parents told us that we must eat vegetables to be healthy.
And it turns out, our parents were right!
So, what else did our parents tell?
def showInLines():
# Open the file STORY.TXT for reading.
with open("STORY.TXT", "r") as file:
content = file.read() # Read the entire file content.
sentences = content.split('. ') # Split into sentences based on '. '.
# Further process to correctly handle sentences ending with '?' or '!'.
final_sentences = []
for sentence in sentences:
# Split by '? ' if present, otherwise by '! '.
sub_sentences = sentence.split('? ') if '?' in sentence else sentence.split('! ')
final_sentences.extend(sub_sentences)
# Print each sentence on a new line.
for sentence in final_sentences:
print(sentence.strip()) # Remove leading/trailing whitespace.
Explanation:
The file STORY.TXT is opened in read mode using with open(). The file's content is read into a string and then split into sentences using split('. '). For sentences that contain ? or !, additional splitting is performed to ensure accurate sentence separation. Each sentence is subsequently printed on a new line after removing any extraneous whitespace with strip().
def callon(b=20, a=10):
b = b + a
a = b - a
print(b, "#", a)
return b
x = 100
y = 200
x = callon(x, y)
print(x, "@", y)
y = callon(y)
print(x, "@", y)
A tuple named subject stores the names of different subjects. Write the Python commands to convert the given tuple to a list and thereafter delete the last element of the list.
Write a user-defined function in Python named showGrades(S) which takes the dictionary S as an argument. The dictionary S contains Name: [Eng, Math, Science] as key:value pairs.
The function displays the corresponding grade obtained by the students according to the following grading rules:
\[ \begin{array}{|c|c|} \hline \textbf{Average of Eng, Math, Science} & \textbf{Grade} \\ \hline \geq 90 & A \\ \hline < 90 \text{ but } \geq 60 & B \\ \hline < 60 & C \\ \hline \end{array} \]
Example: Consider the following dictionary: \[ S = \{\text{"AMIT"}: [92, 86, 64], \text{"NAGMA"}: [65, 42, 43], \text{"DAVID"}: [92, 90, 88]\} \] The output should be: \[ \text{AMIT} - B \\ \text{NAGMA} - C \\ \text{DAVID} - A \]