Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you help me my assignment? Its a python script. I am having trouble with thi

ID: 3770514 • Letter: C

Question

Can you help me my assignment? Its a python script. I am having trouble with this script program. i tried adding the number as told to do so in the directions but i am getting stuck somewhere. The directions and script is below. Thank you for any help!!

Modify the script to read from a file rather than the keyboard.   Use the techniques learned from the chapter and the prior tutorials. Be sure to:

Replace the code under the # Read the input values comment with your own code to open and read from a file, appending the numbers in the file to thevalues list.

Skip the requirement that the data ends when ‘Q’ is read .. you can just use afor loop to read the data. The for loop will stop when the end of file is reached.

Print an error message if the file could not be opened.

Using a text editor or Idle, create an input file. The filename should be “numbers.txt” and contain a list of ten numbers, one per line, in no particular order.   You can use the sample numbers shown on page 2.

##
# This program reads a sequence of values and prints them, marking the largest value.
#

# Create an empty list.
values = []

# Read the input values.
print("Please enter values, Q to quit:")
userInput = input("")
while userInput.upper() !="Q" :
values.append(float(userInput))
userInput = input("")


# Find the largest value.
largest = values[0]
for i in range(1, len (values)):
if values[i] > largest :
largest = values [i]


# print all values, marking the largest.
for element in values :
print(element, end="")
if element == largest :
print(" <== largest value" , end="")
print()

A sample series of numbers to place in the numbers.txt file to test the script.

!0

23.5

1

-50

256

5

92

256

9

5

By the way, did you remember to have your code close the file when done reading? (You can skip doing that if you used the “with” syntax shown in the tutorial.)

Explanation / Answer

Code:

# Hello World program in Python
fr = open("numbers.txt","r")
values = []
for line in fr:
ele = float(line.split()[0])# extract number from line(string)
values.append(ele) # append number to values list

# Find the largest value.
largest = values[0]
for i in range(1, len (values)):
if values[i] > largest :
largest = values [i]

# print all values, marking the largest.
for element in values :
print(element, end="")
if element == largest :
print(" <== largest value" , end="")
print()

Output:

0.0   

23.5   

1.0   

-50.0

256.0 <== largest value

5.0

92.0

256.0 <== largest value

9.0

5.0   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote