Menu

[Solved]1 Question Write Python Program Use Else Condition Statement Calculate Sum Odd Numbers Ave Q37261492

1. Question: Write a Python program to use if-elsecondition statement to calculate the SUM of oddnumbers, and the AVERAGE of even numbers,from numbers (more than 10 random numbers) within anexisting .txt file.  (the .py file and .txt fileshould be saved in the same location.) See Zelle’stextbook Page 243, average5.py, or view fromChromehttps://mcsp.wartburg.edu/zelle/python/ppics2/code/chapter08/average5.py

# average5.pydef main(): fileName = input(“What file are the numbers in? “) infile = open(fileName,’r’) sum = 0.0 count = 0 for line in infile: sum = sum + eval(line) count = count + 1 print(“nThe average of the numbers is”, sum / count)main()

2. Hint 1: if-else condition: See Zelle’s textbook Page 234,average1.py, or view from Chromehttps://mcsp.wartburg.edu/zelle/python/ppics2/code/chapter08/average1.py

# average1.pydef main(): n = eval(input(“How many numbers do you have? “)) sum = 0.0 for i in range(n): x = eval(input(“Enter a number >> “)) sum = sum + x print(“nThe average of the numbers is”, sum / n)main()

3. Hint 2: use numeric operation % to know ifthe number is odd or even, see textbook Page 58

Expert Answer


Answer to 1. Question: Write a Python program to use if-else condition statement to calculate the SUM of odd numbers, and the AVER… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *