Homework (counting iteration issue) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Homework (counting iteration issue) (/thread-8551.html) Pages:
1
2
|
Homework (counting iteration issue) - Cardinal07 - Feb-25-2018 Below is my Homework assignment. I cannot for the life of me figure out why my while loop doesn't recognize my condition != 'no'. The loop will not exit when 'no' is entered. ANY insight would be great! # This program will ask a user to input the length of # three sides to a triangle.When the user inputs the # side lengths the program will determine whether # the triangle is Isosceles, Equilateral, Not equilateral or isosceles. # The program will then give the total number of triangles processed. def main(): # Initialize counter answer = 0 # Get the user to answer yes or no. userAnswer = str(input("Would you like to enter three sides to a" + \ "triangle? Please answer yes or no:")) # Gather side data side1 = int(input("Side 1 Size?")) side2 = int(input("Side 2 Size?")) side3 = int(input("Side 3 Size?")) # Calculate side data while userAnswer != 'no': answer += 1 if side1 == side2 == side3: print("Equilateral triangle") break elif side1 != side2 != side3: print("Not equilateral or isosceles") break else: print("Isosceles triangle") break print("Processing is complete. You have evaluated", answer, "sets of sides.") main() print("Processing is complete. You have evaluated", answer, "sets of sides.") main() RE: Homework (counting iteration issue) - Larz60+ - Feb-25-2018 It is indeed seeing 'no, but what do you do immediately after getting userAnswer, and before you check if answer was no? RE: Homework (counting iteration issue) - Cardinal07 - Feb-25-2018 Thank you for your input, I am new to programming in general. Let me think on your response and see what I can put together. RE: Homework (counting iteration issue) - Cardinal07 - Feb-25-2018 This is where Ive gotten to. No I get the proper response when inputing 'no' but it's not counting the iterations of my "side inputs loop" also if I do more than three iterations of 'yes" the loops just stops. def main(): # Initialize counter answer = 0 userAnswer= 'yes' # Get the user to answer yes or no and # calculate side data, if yes. while userAnswer != 'no': answer += 1 userAnswer = str(input("Would you like to enter three sides to a " + \ "triangle? Please answer yes or no:")) if userAnswer == 'no': print("Process is complete. You have evaluated", answer, "sets of sides") break side1 = int(input("Side 1 Size?")) side2 = int(input("Side 2 Size?")) side3 = int(input("Side 3 Size?")) if side1 == side2 == side3: print("Equilateral triangle") break if side1 != side2 != side3: print("Not equilateral or isosceles") break else: print("Isosceles triangle") break main() RE: Homework (counting iteration issue) - Larz60+ - Feb-25-2018 Here's a better way to format this, and correct error: def main(): # Initialize counter answer = 0 userAnswer = None while userAnswer != 'no': userAnswer = None while userAnswer != 'yes' and userAnswer != 'no': userAnswer = input("Would you like to enter three sides to a triangle? Please answer yes or no: ") if userAnswer == 'no': print("\nProcess is complete. You have evaluated", answer, "sets of sides") else: side1 = int(input("Side 1 Size? ")) side2 = int(input("Side 2 Size? ")) side3 = int(input("Side 3 Size? ")) if side1 == side2 == side3: print("Equilateral triangle") elif side1 != side2 and side1 != side3: print("Not equilateral or isosceles") else: print("Isosceles triangle") answer += 1 print('\nTotal number of iterations: {}'.format(answer)) main()test:
RE: Homework (counting iteration issue) - Cardinal07 - Feb-25-2018 Wow! Super helpful. I am in the process of comparing your changes to my original code so I can see where I went wrong. So much to learn! Thank you again! RE: Homework (counting iteration issue) - Larz60+ - Feb-25-2018 I made one small change, indented the increment on answer, needed to be in loop RE: Homework (counting iteration issue) - Cardinal07 - Feb-25-2018 Acutally I think your first placement was correct. When I placed it inside the loop it did not correctly display the amount of side "set evaluated". RE: Homework (counting iteration issue) - sparkz_alot - Feb-25-2018 For obvious reasons, we generally do not like giving students the actual answers to their homework assignments. That said, your second code example would work just fine with the relocation of the answer += 1 (Do you see why it needs to move?), changing one conditional (if) statement, and changing one word in three locations. Can you see the difference between this word and it's counterpart?More of a 'save yourself some typing' than an error, by default, the input() function returns a string, so using str(input)) is redundant. RE: Homework (counting iteration issue) - Cardinal07 - Feb-26-2018 Sparkz_alot, Thank you for your suggestions, I'm comparing your comments to my original code and trying to understand where the improvements go. Yes, I do understand that we(students) don't learn as well when the answer is handed out, none the less, it was extremely helpful. |