Question

In: Computer Science

Python On occasion, programs are mis-written such that a loop has no way to exit. This...

Python

  1. On occasion, programs are mis-written such that a loop has no way to exit. This is known as an _______________________.

  2. et_courses = [‘et710’, ‘et712’, ‘et710’, ‘et710’, ‘et574’, ‘et575’, ‘et710’, ‘et574’]
    Write a python program (include a while loop) to remove the multiple occurrences of ‘et710’ from the list above. Print the beginning list as well as the modified list to demonstrate that it worked.
    --OUTPUT--
    Before:
    [‘et710’, ‘et712’, ‘et710’, ‘et710’, ‘et574’, ‘et575’, ‘et710’, ‘et574’]
    After:
    [ ‘et712’, ‘et574’, ‘et575’, ‘et574’]

----------------

3. Write Python program which asks the user to name a favorite movie.

Print a message for each movie the user enters.
Allow the user to enter ‘quit’ in order to exit.
Reference: https://github.com/ehmatthes/pcc/blob/master/chapter_07/cities.py

---OUTPUT---
Please tell me one of your favorite movies: (Enter 'quit' when you are finished.) Jaws Jaws was one of my favorite movies too!

Please tell me one of your favorite movies: (Enter 'quit' when you are finished.) 1917 1917 was one of my favorite movies too!

Please tell me one of your favorite movies:
(Enter 'quit' when you are finished.) No Country for Old Men No Country for Old Men was one of my favorite movies too!

Please tell me one of your favorite movies: (Enter 'quit' when you are finished.) quit

----------------------------------

4. Write a python program which polls the user to provide the names of subjects they have studied which required a lot of study time. For each, ask them where they studied this subject.
When the user quits the program, print their responses as shown in the example output below. Reference: https://github.com/ehmatthes/pcc/blob/master/chapter_07/mountain_poll.py

Tell me about a subject you studied which required a lot of study time? piano
Where did you study it? Julliard
Would you like to tell us about another subject which required a lot of study time? (yes/ no) yes Tell me about a subject you studied which required a lot of study time? Chemistry
Where did you study it? High School
Would you like to tell us about another subject which required a lot of study time? (yes/ no) y Tell me about a subject you studied which required a lot of study time? Python
Where did you study it? QCC

Would you like to tell us about another subject which required a lot of study time? (yes/ no) no

---OUTPUT---
--- Poll Results ---

In Julliard you studied piano.
In High School you studied Chemistry. In QCC you studied Python.

Solutions

Expert Solution

Ans 1: Infinite loop

Python code for remaining problems below:

et_courses = ['et710', 'et712', 'et710', 'et710', 'et574', 'et575', 'et710', 'et574']

print (et_courses)

item_to_remove = et_courses [0];
while item_to_remove in et_courses:
    et_courses.remove (item_to_remove)
    
print (et_courses)
#---------------------------------------------

movie = ""
while (movie != "quit"):
    movie = input ("Please tell me one of your favorite movies: (Enter 'quit' when you are finished: ")
    if movie != "quit":
        print (movie + " was one of my favorite movies too!")
#-----------------------------------------------        
subjects = []
schools = []
contnue = "yes"
while contnue == "yes":
    subjects.append (input ("Tell me about a subject you studied which required a lot of study time? ") )
    schools.append (input ("Where did you study it? "))
    contnue = input ("Would you like to tell us about another subject which required a lot of study time? (yes/ no)")

print ("POLL RESULTS")
for i in range (len(subjects)):
    print ("In {} you studied {}".format (schools[i], subjects[i]))

Related Solutions

Why is an exit condition so important for a while loop?
Why is an exit condition so important for a while loop?a.An exit condition is not vital.  A while loop works fine without one.b.So the programmer knows when it is okay to leave the room.c.If no exit condition is provided, while loops cannot continue beyond one iterationd.It allows the loop to end instead of running indefinitely.e.While loops are unstable, and must know how to exi
Python Loop invariant To show that an assertion A is a loop invariant , is it...
Python Loop invariant To show that an assertion A is a loop invariant , is it enough to argue that the execuation of the loopbody preserves A? Please desscribe it throughly.
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
python Write a Loop class. The contents of a loop object are represented internally as a...
python Write a Loop class. The contents of a loop object are represented internally as a simple python list with the following additional instance variables: loop - a python list containing the elements of the loop list head - which stores the index of the first element of the loop list current - which stores the index of the last element of the loop list size - which stores how many actual elements are in the loop max - which...
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve week program training to compete in a triathlon. The triathlon consists of three athletic events, 1.5 k swim, 40k bike, 10k run. In order to be prepared for the competition you want to print a training schedule. Starting with week 1 you will increase the distance of each activity so that you reach the race distance by week twelve. Due to rounding, you may...
David is driving his prius on the freeway and reaches his exit. The exit has a...
David is driving his prius on the freeway and reaches his exit. The exit has a radius of curvature of 25 m and is banked at 38 degrees. How fast can David travel around the curve on a dry day (where the coefficient of friction between his tires and the road is 1)? How fast can he travel around the curve on a rainy day (when the coefficient of friction between his tires and a wet road is 0.75)? Give...
The Objectives are to: Use IDLE IDE for Python. Develop a few simple Python programs and...
The Objectives are to: Use IDLE IDE for Python. Develop a few simple Python programs and show they will translate and run. Experience error messages when you enter bad syntax the interpreter cannot understand. Discussion: The best way to learn Python (or any programming language) is to write & run Python programs. In this lab you will enter two programs, and run them to produce the output. Then deliberately insert syntax errors to see what kinds of error messages you...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop invariant for loop 1: “the contents ofnewlistare in sorted order,newlistcontainsaelements oflistAandbelements oflistB” def mergeSortedLists(listA, listB):  newlist = [ ]  a = 0  b = 0  # loop 1  while a < len(listA) and b < len(listB):   if listA[a] < listB[b]:    newlist.append(listA[a])    a +=1   else:    newlist.append(listB[b])    b +=1  if a < len(listA):   newlist.extend(listA[a:])  if b < len(listB):   newlist.extend(listB[b:])  return newlist (a) Write down (in regular...
Occupancy data from a single loop detector was obtained from a one way street. The loop...
Occupancy data from a single loop detector was obtained from a one way street. The loop detector has a length of 6 feet and was observed to have six vehicles cross over it in a period of 30 seconds. The duration and the length of each vehicle are shown in the table below. Use the average vehicle length in your calculations. Estimate the values of q, k and u (flow, density and speed) Vehicle 1 2 3 4 5 6...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT