The splitO method cuts the string

Imagine you have a string containing several words assigned to a variable. Think of a variable as if it's a labeled jar.

rock_band = "Al Carl Mike Brian"

Contains four words-

ft single fiable « assigne ft variable, a labeled jar-

The rock band string, like all Python strings, has a split () method that returns a collection of substrings. one for each word in the original string.

Using a programming feature called multiple assignment, you can take the result from the cut performed by split () and assign it to a collection of variables.

The left side of the (rhythm, lead, vocals, bass) = rock_band.split()

assignment operator lists / the variables to assign values to.

Contained a variable

Contained a variable

The right side of the assignment operator contains the call to the splitO method-

Al

Carl

Mike

Brian

1

•each with its own stringed value.

Multiple variables-

•each with its own stringed value.

Each of the return values from the split () on rock band is assigned to its own separately named variable, which allows you then to work with each word in whatever way you want. Note that the rock band variable still exists and that it still contains the original string of four names.

Looks like you can use multiple assignment and split() to extract the scores from the results. txt file.

Here is the current version of the program:

highest_score = 0 result_f = open("results.txt") for line in result_f:

if float(line) > highest_score: highest_score = float(line) result_f.close() print("The highest score was:") print(highest_score)

Write the extra code required to take advantage of the split () method and multiple assignment in order to create variables called name and score. Then use them to complete the program to find the highest score.

Exercise find the top score

Here is the current version of the program:

highest_score = 0

result_f = open("results.txt")

for line in result_f:

if float(line) > highest_score: highest_score = float(line) result_f.close() print("The highest score was:") print(highest_score)

You were to write the extra code required to take advantage of the split () method and multiple assignment in order to create variables called name and score, then use them to complete the program to find the highest score.

The only Code changes--^ for line in result_f I

required are within the I

for loop. The rest of (name, score) = linesplit() the program remains unchanged. if float(score) > highest_score:

highest_sCore = -float(sCov-e)

You are no longer comparing the line to the highest score, so be sure to compare tta "score" variable instead.

Tqst Drove

So what happens when you run this version of the code within IDLE ? Let's amend the code and see:

amended ¿ode, which tak« advairb9e ihe fl'tO method.

amended ¿ode, which tak« advairb9e ihe fl'tO method.

This time, the program produces a result you can actually use.

It works! The program reads each line from the file as a string, extracts the score using the split () method, and then uses it to find the highest score in the file. The organizers are so excited to hear that the program is finished that they immediately display the result on the large scoreboard at the back of the beach.

more top scores

Was this article helpful?

0 0

Post a comment