Posts: 13
Threads: 2
Joined: Nov 2017
Hello again. First off let me thank you for all your help.
Now, I went back into my code and took a function out and moved some things around. I have it running again with correct validating errors. Only now, it seems that my calculation has gotten mixed up in the process and I am not returning the value(or only the first). Any suggestions would be awesome.
# Constant Integer HOURS = 7
HOURS = 7
def valid_float_number(value):
try:
float(value)
return value
except ValueError:
print("Please try again.")
return False
# Function Real real_number(String prompt)
# Declare String value
#
# Display prompt
# Input value
# While not value is a real number
# Input value
# End While
# Return value
# End Function
def real_number(prompt):
value = ""
value = input(prompt)
while not valid_float_number(value):
value = input(prompt)
return value
if float(value) < 0 or value == "":
print(value, "is less than 0!")
value = input(prompt)
return value
else:
return True
# Function Real get_pints_for_hours()
#
# Display prompt
# Input Real prompt
#
# Return user_input
# End Function
def get_pints_for_hour():
user_input = float(real_number("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)
# Function display_results(average_pints, mimimum, 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)
# End Function
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)
# Function is_yes_or_no(Real value)
#
# Return value == "yes" or value == "no"
def is_yes_or_no(value):
return value == "yes" or value == "Yes" or value == "no" or value == "No"
# Function Boolean end_yes_or_no(String prompt)
# Declare String value
#
# Display prompt
# Input value
# While not is_yes_or_no
# Display prompt
# Input value
# If value == "yes" or value == "YES"
# Return True
# Else
# Return False
# End If
# End While
# End Function
def end_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
# Function prompt_done()
# Display prompt
# Input prompt
#
# Return end_yes_or_no(prompt)
# End Function
def prompt_done():
prompt = "Do you want to end program? (Enter no or yes): "
return end_yes_or_no(prompt)
def main():
done = False
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()
main()
Posts: 14
Threads: 2
Joined: Aug 2017
Nov-22-2017, 07:13 PM
(This post was last modified: Nov-22-2017, 07:13 PM by Prrz.)
For example, and for your own sake please do not copy this in its entirety, here is the code that I made to solve the very same problem. Run it, read through it. I'll look into the code you've provided, however like I mentioned earlier the format is a bit difficult for me to follow but I'll do my best to help debug it (this will take me some extra time, I'll get back to you on it). If you do decide to follow the model I've given to you, I suggest re-writing it in your own style, because I reiterate, copy-and-pasting would do you no good. With that being said, are there any stipulations that were given by your instructor that I should know about? For instance, things you can and cannot do? Minumum requirements etcetera?
# ! Python3
def get_pints():
pints_collected = 0
pints_data = []
counter = 0
done = False
while counter <= 6 and done == False: # counter <= #, # + 1 = number of inputs accepted before calcs
try:
pints = int(input("Enter pints collected: ")) # Accept user input
if pints < 0: # Check if negative
print("You've entered a Negative number.")
else: # If not, add the number to the total and the list
pints_collected += pints # Adds input to total
pints_data.append(pints) # Appends input into a list
counter += 1 # Adds 1 to counter
print(pints_data) # Only for testing purposes, delete later
print(pints_collected) # Only for testing purposes, delete later
except ValueError: # If not a number
print("That is not a valid entry, try again please.")
else:
# 7 is number of inputs taken, change if (counter <= #) is changed
average = round(pints_collected / 7)
maximum = max(pints_data)
minimum = min(pints_data)
print("The average number of pints donated is: ",average)
print("The highest pints donated is ",maximum)
print("The lowest pints donated is ",minimum)
retry = input("End the program? (Yes or No): ").upper() # Eliminates case errors in input
if retry == "NO":
pints_collected = 0 # Reset total
pints_data = [] # Reset list
counter = 0 # Reset counter
return get_pints() # Return original function
else:
print("Goodbye!")
done = True
get_pints() # Start program by calling get_pints function Regards,
Prrz
Posts: 13
Threads: 2
Joined: Nov 2017
The only requirements that my teacher has given is that he wants everything in their own functions and with the min and max, we have to actually code it out and not use the shortcuts. I used them in my code because I wanted to get everything running and go back and put that in. There are a lot of YouTube videos out there. I know my code is everywhere and I appreciate you helping me along with this. As for your code above, I am going to just read it and probably break things down in their own functions and see how I can compare it to my work.
Posts: 13
Threads: 2
Joined: Nov 2017
Hello again, ok so I rewrote all my code in hopes that I could make it more readable to everyone. I am still battling my validation problem and now min and max won't work. Also, when the user puts in a negative number, instead of looping and inputting the new value, it loops and adds the negative number in its place.
Here is my updated code (I've taken out min and max for the time being):
PINTS_DATA = 6
def is_actual_float(pints):
try:
is_actual = float(pints)
if is_actual < 0:
return False
is_actual = True
except ValueError:
is_actual = False
return is_actual
def get_pints_for_drive():
pints = 0.0
pints_collected = 0.0
PINTS_DATA = []
counter = 0
while counter <= 6:
pints = float(input("Enter pints collected: "))
is_actual = is_actual_float(pints)
counter += 1
pints_collected += pints
PINTS_DATA.append(pints)
while not is_actual:
print("Please try again.")
pints = float(input("Enter pints collected: "))
is_actual = is_actual_float(pints)
input_float = pints
print(pints_collected)
return pints_collected
def calculate_average_pints(pints_collected):
average_pints = pints_collected / 7
return average_pints
def is_yes_or_no(value):
return value == "yes" or value == "Yes" or value == "no" or value == "No"
def prompt_done():
prompt = "Do you want to end program? (Enter no or yes): "
return end_yes_or_no(prompt)
def end_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 display_information(average):
print("The average of pints collected is: ", average)
# print("The minimum number of pints collected: ", minimum_pints)
# print("The maximum number of pints collected: ", maximum_pints)
def main():
done = False
while not done:
pints_collected = get_pints_for_drive()
average_pints = calculate_average_pints(pints_collected)
display_information(average_pints)
done = prompt_done()
main()
Posts: 14
Threads: 2
Joined: Aug 2017
Nov-23-2017, 08:07 PM
(This post was last modified: Nov-23-2017, 08:08 PM by Prrz.)
The issue with validating is within your get_pints_for_drive() function. Firstly, I'd suggest using lower-case for the PINTS_DATA list, for sake of clarity. To put it simply, you'd want to check if the number is negative before appending to your list, adding to the counter, and adding to the total. The best way to do this would be to remove your second while statement and embed an if statement with the else being when a negative number is inputted.
This should fix your problem with validation and only append numbers that are positive, otherwise "Plese try again" will print.
def get_pints_for_drive():
pints = 0.0 # Don't need this
pints_collected = 0.0
PINTS_DATA = [] # Would make PINTS_DATA lowercase
counter = 0
while counter <= 6:
pints = float(input("Enter pints collected: "))
if is_actual_float(pints):
# If it passes the is_actual_float function, then append, add counter etc.
else:
# If it doesn't pass, print something, it will automatically keep trying the while loop
return pints_collected As for the min and max issues, since you aren't able to use the predefined min() and max(), you'd have to create your own functions for this. You can do this is multiple ways, you can arrange the list using list.sort() which will put the smallest number in the first position, and the largest number in the last position. From there you could pull the value from the first and last positions using list[0] and list[6], effectively killing two birds with one stone. Since everything is nestled in their own functions, this could become a bit tricky, but with some tinkering, I think it would work.
Another solution would be to loop through the list, like so.
def highestNumber(l):
myMax = l[0]
for num in l:
if myMax < num:
myMax = num
return myMax
print highestNumber ([77,48,19,17,93,90])
Posts: 13
Threads: 2
Joined: Nov 2017
I changed up my code to take away the extra while loop and include it in the first while statement when the user puts in a negative it works great. However, when they put in a string I receive a ValueError like below. I have a feeling it has to do with my is_actual_float function but I can't seem to figure out where. I tried changing things around but it didn't work.
Traceback (most recent call last):
File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 82, in <module>
main()
File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 75, in main
pints_collected = get_pints_for_drive()
File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 24, in get_pints_for_drive
pints = float(input("Enter pints collected: "))
ValueError: could not convert string to float: 'Im lost' Also, I tried your second recommendation with the max and min and for some reason, I keep getting an error message whether it be the format you gave me or the python shortcuts. I know your format is correct along with the python shortcuts. In short, I just need a quick explanation of what the following error message means. I've looked on the internet and just cannot find a concrete answer.
Traceback (most recent call last):
File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 82, in <module>
main()
File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 77, in main
maximum = max(pints_collected)
TypeError: 'float' object is not iterable
Posts: 14
Threads: 2
Joined: Aug 2017
Nov-28-2017, 11:20 PM
(This post was last modified: Nov-28-2017, 11:20 PM by Prrz.)
(Nov-24-2017, 11:41 PM)Austin11 Wrote: I changed up my code to take away the extra while loop and include it in the first while statement when the user puts in a negative it works great. However, when they put in a string I receive a ValueError like below. I have a feeling it has to do with my is_actual_float function but I can't seem to figure out where. I tried changing things around but it didn't work.
Traceback (most recent call last):
File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 82, in <module>
main()
File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 75, in main
pints_collected = get_pints_for_drive()
File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 24, in get_pints_for_drive
pints = float(input("Enter pints collected: "))
ValueError: could not convert string to float: 'Im lost' Also, I tried your second recommendation with the max and min and for some reason, I keep getting an error message whether it be the format you gave me or the python shortcuts. I know your format is correct along with the python shortcuts. In short, I just need a quick explanation of what the following error message means. I've looked on the internet and just cannot find a concrete answer.
Traceback (most recent call last):
File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 82, in <module>
main()
File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 77, in main
maximum = max(pints_collected)
TypeError: 'float' object is not iterable
For the ValueError, this is indeed a problem with your is_actual_float function, or rather a problem with it not being able to check for a string using the Try and Except method reliably. To fix this is easy enough, remove the "try" and "except" from the is_actual_float function and move it to your get_pints_for_drive function as follows:
try:
pints = int(input("Enter pints collected: "))
if is_actual_float(pints):
counter += 1
pints_collected += pints
pints_data.append(pints)
pints_data.sort()
else:
print("Please try again.")
except ValueError:
print("Please try again.") As for the min and max error, the error you are getting is because floats are not iterable, and max() is trying to iterate through pints_collected, which in my script is a float, not a list, you'd have to call max(pints_data) for that to work. (Floats are not iterable)
I've done this for my own testing and everything seems to be working perfectly, this obviously isn't the complete code just snippets of what is relevant. Side note, I would use [-1] to call the last number in our list instead of [6].
def get_pints_for_drive(pints_data = [], pints_collected = 0):
# Python Stuff
return pints_collected, pints_data
def get_average(pints_collected):
average_pints = round(pints_collected / 7,2)
return average_pints
def get_max(pints_data):
max_pints = pints_data[-1]
return max_pints
def get_min(pints_data):
min_pints = pints_data[0]
return min_pints
def display_information(average,maximum,minimum):
print(average,maximum,minimum)
def main():
done = False
while not done:
pints_collected, pints_data = get_pints_for_drive()
average = get_average(pints_collected)
maximum = get_max(pints_data)
minimum = get_min(pints_data)
display = display_information(average,maximum,minimum)
done = prompt_done()
|