Aug-17-2020, 10:40 AM
The comments in my script says it all, but to summarize, sometimes when this code adds say "5.24 or 3.46" or any other non integer number to the calculation it causes the result to be something like ex. 56.789999999996 when it "should" be only 2 decimals like 5.00.
#this script is an attempt to create a simple shopping calculator #I am brand new to programming and am well aware there may be #much better ways to do this. #I am open to suggestions to different methods of reaching this goal #but also an explanation to why this is happening and possibly how to #fix it in this context would be much appreciated. #gets user input and adds it to total. while True: #gets user input user_input=input("") #stops an error where no input causes the next if statement to try to add a string to an integer if not user_input: user_input=0 #converts user input from a string to a float and adds it to the total. #for some reason unknown to me, this function sometimes causes the total to become #something like ex. 54.999999999996 after adding some non int inputs like ex. 5.23? #it probably has something to do with how python converts a string to a float? maybe? if user_input: total=total + float(user_input) print(total,"+")