Question

Can anyone explain this code line by line, what it is, what is it doing? lst...

Can anyone explain this code line by line, what it is, what is it doing?

lst = []
print("Enter 10 numbers: ") #flag indication if a specfic condition exist
flag = False #
for x in range(10):
num = eval(input())
if(num >= 100):
flag = True
lst.append(num)
if(flag):
print("Warning: value over 100 has been entered.")
maxVal = lst[0]
minVal = lst[0]
total = 0
avg = 0
for y in lst:
if(maxVal < y):
maxVal = y
if(minVal > y):
minVal = y
total += y

print("Maximum =",maxVal)
print("Miniimum =",minVal)
print("Average =",(total/10))

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello, I have explained every statement using comments. Please check and let me know if you have any doubts. Thanks.

I have also attached screen shot of the code also. If any compilation error related to Code indentation occurs please compare and correct the code with screen shot.

CODE

"""
In this program user inputs 10 numbers
and prints maximum, minimum and average value of
input numbers
"""

#creating a list named lst
#list is a collection which is ordered and changeable.
lst = []

#printing prompt for 10 numbers
#print() function prints the specified message to the screen
print("Enter 10 numbers: ")

#set flag as false
#flag indication if a specfic condition exist
flag = False

#inputs numbers using for loop with range
#start from index 0 to index 9
for x in range(10):
  
   #inputs number
   #eval is used to evaluate input
   num = eval(input())
  
   #if any number is greater than zero
   if(num >= 100):
       #set flag as true
       flag = True

   #appending number to list lst
   lst.append(num)

#if flag is true
if(flag):
   #print warning message
   print("Warning: value over 100 has been entered.")

#set maximum value as value in index 0 of lst  
maxVal = lst[0]

#set minimum value as value in index 0 of lst  
minVal = lst[0]

"""
We can't set maximum and minimum value as zero
if all values in the list are negative
it will make logical error
"""

#set total as zero
total = 0

#set average as zero
avg = 0

#iterates each element in list
for y in lst:
   #if current index value is greater than maxVal
   if(maxVal < y):
       #set maxVal as current index value
       maxVal = y
      
   #if current index value is less than minVal
   if(minVal > y):
       #set minVal as current index value
       minVal = y
  
   #adding current index value to total
   total += y

#printing maximum value
print("Maximum =",maxVal)

#printing minimum value
print("Miniimum =",minVal)

#printing average of 10 numbers
print("Average =",(total/10))

OUTPUT

CODE SCREEN SHOT

Add a comment
Know the answer?
Add Answer to:
Can anyone explain this code line by line, what it is, what is it doing? lst...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have this code for python: num = int(input()) if num == 0: print('No change') else:...

    I have this code for python: num = int(input()) if num == 0: print('No change') else: dol = num // 100 num %= 100 Quarters = num // 25 num %= 25 Dimes = num // 10 num %= 10 Nickels = num // 5 num %= 5 Pennies = num if dol > 0: if dol == 1: print('1 Dollar') if Dollars > 1: print(dol,'Dollars') if Quarters > 0: if Quarters == 1: print('1 Quarter') if Quarters > 1:...

  • Can someone provide detailed comments on what this code is doing for each line? I posted...

    Can someone provide detailed comments on what this code is doing for each line? I posted the assignment below. I am having trouble learning and getting assistance because I keep getting answers provided w/ good information on how the problem was solved. I have spent too much on tutoring this month and I am relying heavily on assistance here. Can you please assist with helping me understand each line of code that was written here? Also- is there an easier...

  • can someone please comment through this code to explain me specifically how the variables and arrays...

    can someone please comment through this code to explain me specifically how the variables and arrays are working? I am just learning arrays code is below assignment C++ Programming from Problem Analysis to Program Design by D. S. Malik, 8th ed. Programming Exercise 12 on page 607 Lab9_data.txt Jason, Samantha, Ravi, Sheila, and Ankit are preparing for an upcoming marathon. Each day of the week, they run a certain number of miles and write them into a notebook. At the...

  • how to make my code of python work probely my q is how to write a...

    how to make my code of python work probely my q is how to write a code to find the average actual water level, average error by using python and my program should be flexible where user can select the column no. If you can see in the main program, it asked which column to know the maximum or average, the column number is passed to the function. The function should look which column to do. #THIS PROGRAMMING IS ABLE...

  • Can someone please explain this piece of java code line by line as to what they...

    Can someone please explain this piece of java code line by line as to what they are doing and the purpose of each line (you can put it in the code comments). Code: import java.util.*; public class Array { private Integer[] array; // NOTE: Integer is an Object. Array() {     super();     array = new Integer[0]; } Array(Array other) {     super();     array = other.array.clone(); // NOTE: All arrays can be cloned. } void add(int value) {    ...

  • Explain what the code is doing line from line explanations please or summarize lines with an explanation def displayCart(): #displays the cart function """displayCart function - dis...

    Explain what the code is doing line from line explanations please or summarize lines with an explanation def displayCart(): #displays the cart function """displayCart function - displays the items in the cart ---------------------------------------------------------------------""" import os os.system('clear') print("\n\nCart Contents:") print("Your selected items:", cart) def catMenu(): #function that displays the category menu """catMenu function - displays the categories user picks from ---------------------------------------------------------------------""" import os os.system('clear') print(""" 1 - Books 2 - Electronics 3 - Clothing d - display cart contents x -...

  • To insure that file output is written to the disk you need to execute what method?...

    To insure that file output is written to the disk you need to execute what method? a. commit() b. write() c. close() d. complete() The following is called the ________ of a function. def myFunction(x,y) : a. header b. footer c. sentinel d. index True or False: A commonly used program that uses regular expressions is grep. True or False: In Python exception handling provides a mechanism for passing control from the point of the error detection to a handler...

  • Fix the code using C# shown below to show the repitition as seen on the example...

    Fix the code using C# shown below to show the repitition as seen on the example screenshot: (Invalid error must repeat): using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Program { class Program { static void Main(string[] args) { double num, count = 0; double Tax = 0.00, ship = 5.00, sum = 0.00; double grandtotal; char line; //Set run =true bool run=true; do { //read amount of item Console.WriteLine("What is the amount of item : "); //read in...

  • Can anyone help me write a simple code for this problem in C++? Generate N>10 random...

    Can anyone help me write a simple code for this problem in C++? Generate N>10 random values from 1 to 100 using a uniform probability distribution. N is entered by the user. The first 5 values are put into nodes and added to a priority queue based on their value. Each node also contains a list of values. After the first 5 are inserted into the priority queue the rest of the values are added to the lists with the...

  • Not yet answered Marked out of 1.00 Flag question Question text What is the value of...

    Not yet answered Marked out of 1.00 Flag question Question text What is the value of the following Python expression? not(True and False) Select one: True False Question 2 Not yet answered Marked out of 1.00 Flag question Question text Consider the following Python program. fin = open('words.txt') for line in fin: word = line.strip() print(word) What does the program loop over? Select one: a. Lines in a file b. Lines in a list c. Words in a dictionary d....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT