This program haves to print the avg, highest, lowest and total of the inputs but I am having problems how to make the code print an error message when the input is a negative number. For some reason I don't find the way I don't know if am being too dumb or what
Days = []
day1 = int(input("Enter day 1 rainfall: "))
day2 = int(input("Enter day 2 rainfall: "))
day3 = int(input("Enter day 3 rainfall: "))
day4 = int(input("Enter day 4 rainfall: "))
day5 = int(input("Enter day 5 rainfall: "))
day6 = int(input("Enter day 6 rainfall: "))
day7 = int(input("Enter day 7 rainfall: "))
day8 = int(input("Enter day 8 rainfall: "))
day9 = int(input("Enter day 9 rainfall: "))
day10=int(input("Enter day 10 rainfall: "))
Days.extend((day1, day2, day3, day4, day5, day6, day7, day8, day9, day10 ))
def lowest():
print('The minimum rainfall is', min(Days))
lowest()
def highest():
print('The most rainfall is', max(Days))
highest()
def total():
print('The total rainfall is', sum(Days))
total()
def average():
print('The average rainfall is', float(sum(Days))/len(Days))
average()
CodePudding user response:
Assuming You need 10 inputs, if user enter wrong input, you want to retake that input from user.
Days = []
while True:
day = int(input(f"Enter day {len(Days) 1} rainfall: "))
if day < 0:
print("Invalid Input")
continue
Days.append(day)
if len(Days) == 10:
break
def lowest():
print('The minimum rainfall is', min(Days))
def highest():
print('The most rainfall is', max(Days))
def total():
print('The total rainfall is', sum(Days))
def average():
print('The average rainfall is', float(sum(Days))/len(Days))
lowest()
highest()
total()
average()
CodePudding user response:
I would put your statements asking for user input in a loop, and in the loop you can check if the number is negative, as follows:
Days = []
for count in range(1, 11):
day = int(input("Enter day " count " rainfall: "))
if day < 0:
print("Error")
# You can handle the error here
Days.append(day)
def lowest():
print('The minimum rainfall is', min(Days))
lowest()
def highest():
print('The most rainfall is', max(Days))
highest()
def total():
print('The total rainfall is', sum(Days))
total()
def average():
print('The average rainfall is', float(sum(Days))/len(Days))
average()
CodePudding user response:
Automated way
Days = []
days_dict ={}
for days in range(1,11):
day = int(input(f"Enter day {days} rainfall "))
if day <0:
while True:
print("Days cannot be negative")
day = int(input(f"Enter day {days} rainfall: "))
if day >=0:
break
days_dict[f"day_{days}"] = day
Days.extend([v for k,v in days_dict.items()])
def lowest():
print('The minimum rainfall is', min(Days))
lowest()
def highest():
print('The most rainfall is', max(Days))
highest()
def total():
print('The total rainfall is', sum(Days))
total()
def average():
print('The average rainfall is', float(sum(Days))/len(Days))
Manual Way
Days = []
day1 = int(input("Enter day 1 rainfall: "))
if day1 <0:
while True:
print("Days cannot be negative")
day1 = int(input("Enter day 1 rainfall: "))
if day1>=0:
break
day2 = int(input("Enter day 2 rainfall: "))
if day2 <0:
while True:
print("Days cannot be negative")
day2 = int(input("Enter day 2 rainfall: "))
if day2>=0:
break
day3 = int(input("Enter day 3 rainfall: "))
if day3 <0:
while True:
print("Days cannot be negative")
day3 = int(input("Enter day 3 rainfall: "))
if day3>=0:
break
day4 = int(input("Enter day 4 rainfall: "))
if day4 <0:
while True:
print("Days cannot be negative")
day4 = int(input("Enter day 4 rainfall: "))
if day4>=0:
break
day5 = int(input("Enter day 5 rainfall: "))
if day5 <0:
while True:
print("Days cannot be negative")
day5 = int(input("Enter day 5 rainfall: "))
if day5 >=0:
break
day6 = int(input("Enter day 6 rainfall: "))
if day6 <0:
while True:
print("Days cannot be negative")
day6 = int(input("Enter day 6 rainfall: "))
if day6 >=0:
break
day7 = int(input("Enter day 7 rainfall: "))
if day7 <0:
while True:
print("Days cannot be negative")
day7 = int(input("Enter day 7 rainfall: "))
if day7 >=0:
break
day8 = int(input("Enter day 8 rainfall: "))
if day8 <0:
while True:
print("Days cannot be negative")
day8 = int(input("Enter day 8 rainfall: "))
if day8 >=0:
break
day9 = int(input("Enter day 9 rainfall: "))
if day9 <0:
while True:
print("Days cannot be negative")
day9 = int(input("Enter day 9 rainfall: "))
if day9 >=0:
break
day10=int(input("Enter day 10 rainfall: "))
if day10 <0:
while True:
print("Days cannot be negative")
day10 = int(input("Enter day 10 rainfall: "))
if day10 >=0:
break
Days.extend([day1, day2, day3, day4, day5, day6, day7, day8, day9, day10 ])
def lowest():
print('The minimum rainfall is', min(Days))
lowest()
def highest():
print('The most rainfall is', max(Days))
highest()
def total():
print('The total rainfall is', sum(Days))
total()
def average():
print('The average rainfall is', float(sum(Days))/len(Days))
average()