(Nov-21-2017, 05:11 PM)Austin11 Wrote: [ -> ]I made enhancements to my code and I have it validating every input correctly. My only thing now is that I when they input a string the code closes out nicely. That is great but is there a way to call the "value" function so it can loop until the user inputs a valid number?
Also, I am with you 100%, I don't want to be given the answers just guided in the right direction, I will never learn from that. It just sucks because the book doesn't show any examples of what are assignments actually are.
Here is my updated code:
HOURS = 7
def valid_float_number(value):
float(value)
return True
def valid_value(value, prompt):
while not valid_float_number(value):
value = input(prompt)
return value
if float(value) < 0 or value == "":
print(value, "is less than 0!")
return False
else:
return True
def get_real(prompt):
value = ""
value = input(prompt)
while not valid_value(value, prompt):
value = input(prompt)
return value
def get_pints_for_hour():
user_input = float(get_real("Enter pints collected: "))
return user_input
def get_pints_for_drive():
pints_collected = [0.0 for x in range(HOURS)]
counter = 0
while counter < HOURS:
pints_collected[counter] = get_pints_for_hour()
counter += 1
return pints_collected
def calculate_average_pints(pints_collected_during_drive):
total_pints = 0
counter = 0
while counter < HOURS:
total_pints += pints_collected_during_drive[counter]
counter += 1
return float(total_pints / HOURS)
def display_results(average_pints, minimum, maximum):
print("The average number of pints donated is ", "{:.2f}".format(average_pints))
print("The highest pints donated is ", maximum)
print("The lowest pints donated is ", minimum)
def is_yes_or_no(value):
return value == "yes" or value == "no"
def yes_or_no(prompt):
value = ""
value = input(prompt)
while not is_yes_or_no(value):
value = input(prompt)
if value == "yes":
return True
else:
return False
def prompt_done():
prompt = "Do you want to end program? (Enter no or yes): "
return yes_or_no(prompt)
def main():
done = False
try:
while not done:
pints_collected_during_drive = get_pints_for_drive()
average_pints = calculate_average_pints(pints_collected_during_drive)
minimum_pints = min(pints_collected_during_drive)
maximum_pints = max(pints_collected_during_drive)
display_results(average_pints, minimum_pints, maximum_pints)
done = prompt_done()
except ValueError:
print("Please try again.")
return main
main()
Hello again, I see what you mean. Your code currently will reset itself upon receiving a ValueError, making your prior inputs invalid. To fix this I'd highly suggest completely re-organizing your script. Start from scratch, and think of a simpler way to do this (for sake of example, I rewrote this using 31 lines and only 1 function). I appreciate complexity, however, in coding, it's often the simpler solutions that make the most sense. I do have a hard time understanding your code because it does jump around quite a bit, making it hard to follow what goes where and what does what.
Here's my suggestion, start by defining one function, in my case I named mine
def get_pints()
which requires no arguments. In this function, I defined 4 separate variables, one to hold the total number of pints collected (for ease of calculating sum). Next, I defined a blank list (to keep track of every input given, appending it to a list so each number has its own place). Then I defined a counter, much like you did, to easily keep track of how many inputs have been given. Lastly, I defined a boolean, like you did, to register whether to continue the script or not.
To start you off, here is what the beginning of the function looks like:
def get_pints():
pints_collected = 0
pints_data = []
counter = 0
done = False
while counter <= 6 and done == False:
try:
# Python Stuff
except ValueError:
print("That is not a valid entry, try again please.")
pass
else:
# Do some calculations once we've entered all 7 inputs
# After calculations, ask if user wants to try again, if they do, reset the variables!
With the counter only being able to count up to six, this means everytime I enter a number into the script I make the counter raise by 1, leaving seven places for input, just like your original script (0,1,2,3,4,5,6). After calculations, when asking the user if they wish to exit the program, if answered "yes" you can assign done = True, leaving the while statement unable to execute. If answered "No" you can assign the variables back to their original values, and return to the initial function.
Under the try statement, you would accept the user's input, and you would want to convert it to an integer on the same line. After this, add the input given by the user to the (my_input += pints_collected) "pints_collected" variable, so we can easily define a total number. An important step, is you also want to append the user's input to the blank list "pints_data", this will give each input a unique spot on the list.
Using this method the calculations will be quite simple, and just like the ones you've already used.
else:
average = round(pints_collected / 7) # round the number, take the average.
maximum = max(pints_data) # What's the highest value in our list?
minimum = min(pints_data) # What's the lowest value in our list?
# Print your values
retry = input("Retry? (Yes or No").upper()
# Ask if user wants to go again, upper() makes their input in uppercase.
# So when definining your if statement, make sure you use capitlals.
# (if the_input == "YES)
If you have any questions, feel free to ask!
Regards,
Prrz