203k views
2 votes
Python

Given the variables name1 and name2, write a fragment of code that assigns the larger of their associated values to first.


(NOTE: "larger" here means alphabetically larger, not "longer". Thus, "mouse" is larger than "elephant" because "mouse" comes later in the dictionary than "elephant"!)

1 Answer

6 votes

Let us consider that there are 3 variables “first” to store the content of variable whose value is larger as per the given example in the question, a variable called “name1” to get the first string and “name 2” to get second string. We must run a simple for loop to achieve the result.

if name1>name2

first =name1

else

first=name2

So here like numbers, strings are checked to find which is largest and assigns accordingly. Thus the task is accomplished.

User AlexJF
by
5.7k points