Implement the function printTwoLargest that inputs an arbitrary number of positive numbers from the user. The input of numbers stops when the first negative or zero value is entered by the user. The function then prints the two largest values entered by the user. If fewer than two distinct positive numbers are entered a message to that effect is printed instead of printing any numbers. Hint: Duplicates will cause problems. Try to make sure that you find a way to ignore them.

Answers

Answer 1

Answer:

The function in Python is as follows:

def printTwoLargest():

   chk = 0

   list1 = []

   num = int(input("Enter: "))

   while num > 0:

       for i in list1:

           if i == num:

               chk+=1

               break;

       if chk == 0:

           list1.append(num)

       chk = 0

       num = int(input("Enter: "))

   list1.sort()

   if len(list1) >= 2:

       print("Largest:", list1[-1])

       print("Second:", list1[-2])

   else:

       print("Length of list must be at least 2")

Explanation:

This defines the function

def printTwoLargest():

This initializes a check variable to 0

   chk = 0

This initializes an empty list

   list1 = []

This prompts the user for input

   num = int(input("Enter: "))

The following loop is repeated until input is 0 or negative

   while num > 0:

The following for loop checks for duplicate

       for i in list1:

           if i == num: If duplicate is found

               chk+=1 The check variable is set to 1

               break; And the for loop is exited

The input is appended to the list if the check variable is 0 (i.e. no duplicate)

       if chk == 0:

           list1.append(num)

This sets the check variable back to 0, for another input

       chk = 0

This prompts the user for another input

       num = int(input("Enter: "))

This sorts the list

   list1.sort()

This prints the two largest if valid user input is 2 or more

   if len(list1) >= 2:

       print("Largest:", list1[-1])

       print("Second:", list1[-2])

if otherwise, this prints that the length must be at least 2

   else:

       print("Length of list must be at least 2")


Related Questions

HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP

Answers

Answer:

I should be C! :D

Answer:

B

Explanation:

Other Questions
Confession of____removes the cause of spiritual suffering and clears the channel by which God is able to bless His children.Humility SinGrief What were the drawbacks of child labor? Will give Brainliest (if correct). How did Paul Robeson contribute to the Red Scare? help!!!! ill give u braillist and all if u do all of them!_______________________________________________1. A man hits a golf ball (0.24 kg) which accelerates at a rate of 20 m/s2. What amount of force acted on the ball?2. You give a shopping cart a shove down the aisle. The cart is full of groceries and has a mass of 18kg. The cart accelerates at a rate of 3m/s2. How much force did you exert on the cart?3.The wind pushes a paper cup along the sand at a beach. The cup has a mass of 25 grams (= ? kg) and accelerates at a rate of 5 m/s2. How much force (in Newton's) is the wind exerting on the cup?4. You push a friend sitting on a swing. She has a mass of 50 kg and accelerates at a rate of 4 m/s2. Find the force you exerted. 5. How much force would it take to push another, a larger friend who has a mass of 70kg to accelerate at the same rate of 4 m/s2?6. A worker drops his hammer off the roof of a house. The hammer has a mass of 9kg, and gravity accelerates it at the usual 9.8 m/s2. How much force does the earth apply to the hammer?7. A car whose mass is 1000kg is travelling at a constant speed of 10 m/s. Neglecting any friction, how much force will the engine have to supply to keep going at the same speed? (tricky question, think INERTIA, look at the units) Brooke has been asked to present on the topic below.Persuade students to participate in the annual seventh-grade Fun Run.What would be the most effective claim for this presentation?a. Join in the Fun Run to help a good cause, make friends, and get healthy.b. If seventy-five percent of the seventh-grade runs, we will earn money for gym equipment.c. Students at Sandburg Middle School held a Fun Run last September.d. Studies show that students who exercise perform well in classwork. In a class of 30 students, 13 have a brother and 8 have a sister. There are 3 students who have a brother and a sister. What is the probability that a student who has a sister also has a brother?Answer 3/8 In any piece of literature, a characters tone of voice is usually described by the author after their piece of dialogue. Why do you think this detail is important for understanding the story? Without dialogue inflections, do you think a reader would be able to understand more or less about the characters and their moods in certain situations? Why?(Example: Oh, heck, I know it Randy half-sobbed (117).)I NEED EXAMPLES! GIVING BRAINLIEST! Virginia had an average rainfall of 24 inches last year. This year it rained 10% more than last year. How many inches of rain fell this year? HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP find m13512555100 "The third great goal is to continue the effort so dramatically begun last year: to restore and enhance our natural environment... I will propose a strong new set of initiatives to clean up our air and water, to combat noise, and to preserve and restore our surroundings. I will propose programs to make better use of our land, to encourage a balanced national growth... that will revitalize our rural heartland and enhance the quality of life in America. And not only to meet today's needs but to anticipate those of tomorrow, I will put forward the most extensive program ever proposed by a President of the United States to expand the Nation's parks, recreation areas, open spaces, in a way that truly brings parks to the people where the people are. For only if we leave a legacy of parks will the next generation have parks to enjoy."-from Richard Nixon's 'State of the Union" Address, January 22, 1971Richard Nixon's words here had the GREATEST effect onA)the establishment of the National Park System.B)the creation of the Department of the Interior.C)the political tactic of impounding federal funds.D)the policies of the Environmental Protection Agency (EPA). ZACHARY TAYLOR.1. what major issue did taylor have to deal with when he entered office?2. where did taylor stand with respect to the extension of slavery in the west?MILLIARD FILLMORE.1. did the compromise of 1850 satisfy both sides?2. fillmore was the last president from which political party?FRANKLIN PIERCE.1. what tragedy at the beginning of pierces administration, cast a long shadow over his presidency?2. the passage of what act doomed pierces presidency and brought about significant chaos?JAMES BUCHANAN.1. what position did buchanan take with regard to the dred scott decision?ABRAHAM LINCOLN.1. what bold, unprecedented, moves did lincoln make to grow the power of president in an effort to save the union?2. what was lincolns more profound accomplishment in the office? The path of a flying fish as it made its approach to the surface of the ocean and flew out of the water is represented by the quadratic function h(t) = -t2 + 6t - 5, where t represents the time, in seconds, and h(t) represents the fish's height and depth, in feet. Graph the function using the graphing calculator. To the nearest second, how long was the fish out of the water?1 second3 seconds4 seconds5 seconds NO LINKS Explain how a knee-j erk reflex occurs. which types of change observe the law of conservation of mass? single answer A. only changes of state B. only physical changesC. only chemical changesD. physical changes and chemical changes When abused, opioids can result in death by A. Stopping ones breathing b. Stroke c. Speeding up the heartbeat Evaluate the expression below when n = 2.4. If needed, answer as a fraction or a decimal.12n8 According to the details in Act 1 in The Tragedy of Romeo & Juilet, who is Rosaline? The question is in le picture below U-U Please help I will mark you brainiest What are one advantage and one disadvantage of sexual reproduction?What is one advantage and one disadvantage of asexual reproduction?