Lily C.
asked 01/14/21Python Lists (User Input with all integers)
Hello! I designed this code that takes in user input and creates a list and then outputs the sum, highest number, lowest number, and average. Right now it can only take in whole numbers but I was wondering how I could change it so it could take in decimal numbers(i.e: 1.5) and fractions (i.e: 1/2) as well. Thanks!
list = []
integer = int(input("Hello please enter an integer between 3 and 20: "))
if integer >= 3 and integer <=20:
for i in range(integer):
number = int(input("Enter that number of integers: "))
list.append(number)
print("Integers are: " , list)
print("Sum of all elements in given list: " , sum(list))
print("Highest number: " , max(list))
print("Lowest number: " , min(list))
avg = int(sum(list) / number)
print("Average of all the numbers is: " , avg)
print("Thank you for running this code!")
else:
print("Sorry that is not a good number!")
1 Expert Answer
Patrick B. answered 01/14/21
Math and computer tutor/teacher
# Source code uploaded to RESROUCES section under this link
def parse_fraction(fracBuff):
tokens=fracBuff.split("+")
whole_number=tokens[0]
fracTokens=tokens[1].split("/")
numerator=fracTokens[0]
denominator=fracTokens[1]
if (int(denominator)!=0):
N = int(denominator)*int(whole_number)+int(numerator)
return(float(N)*1.0/int(denominator))
print("Input mixed fractions as whole#+numerator/denominator")
print("EX. 2 and 3/4 input as 2+3/4 ")
amounts = []
N = input("How many statistics are in the data set ??? :>")
if (int(N)<3):
print("Sorry, not enough. Figure it out yourself")
elif (int(N)>20):
print("Sorry, too many !!!")
else:
for i in range(int(N)):
xAmt = input("Please input # :>")
amounts.append(xAmt)
print(amounts)
minVal=float("inf")
maxVal=float("-inf")
totalSum=0
for xAmount in amounts:
if (int(xAmount.find("+"))>-1):
xAmt = parse_fraction(xAmount)
else:
xAmt=xAmount
totalSum = float(totalSum) + float(xAmt)
if (float(xAmt)<float(minVal)):
minVal = xAmt
if (float(xAmt)>float(maxVal)):
maxVal = xAmt
meanAvg = float(totalSum)/int(N)
print("sum is " + str(totalSum))
print("min is " + str(minVal))
print("max is " + str(maxVal))
print(" mean average is " + str(meanAvg))
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
And if the user inputs zero denominator????01/14/21