You are on page 1of 1

Program 1

vals = [None]*10

count = int(input("How many values should be stored in the array? "))

# Validate count

if count <= 0:

print("Invalid input. Count must be a positive integer.")

exit()

for i in range(0, count):

vals[i] = count - i

which = int(input("Which value do you wish to retrieve? "))

# Bounds checking

if which < 0 or which >= count:

print("Index out of bounds. Please enter a valid index.")

else:

print('Your value is', str(vals[which]))

program 2
temps = [None]*10

numTemps = 0

infile = open("temps.txt", "r") # Open temps.txt file in read mode

for line in infile.readlines(): # Read each line from the file

for i in line.split(): # Split each temperature from the line

if numTemps < len(temps): # Check if numTemps is within bounds

temps[numTemps] = float(i) # Assign each temperature to temps buffer

numTemps += 1

else:

break # Break the loop if numTemps exceeds the size of temps

print(str(numTemps) + ' temperatures were read.')

You might also like