Python Forum
Homework (counting iteration issue)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework (counting iteration issue)
#1
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()
Reply
#2
It is indeed seeing 'no, but what do you do immediately after getting userAnswer,
and before you check if answer was no?
Reply
#3
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.
Reply
#4
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()	
Reply
#5
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:
Output:
Would you like to enter three sides to a triangle? Please answer yes or no: yes Side 1 Size? 1 Side 2 Size? 1 Side 3 Size? 1 Equilateral triangle Would you like to enter three sides to a triangle? Please answer yes or no: yes Side 1 Size? 1 Side 2 Size? 2 Side 3 Size? 3 Not equilateral or isosceles Would you like to enter three sides to a triangle? Please answer yes or no: yes Side 1 Size? 1 Side 2 Size? 2 Side 3 Size? 1 Isosceles triangle Would you like to enter three sides to a triangle? Please answer yes or no: no Process is complete. You have evaluated 3 sets of sides Total number of iterations: 4
Reply
#6
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!
Reply
#7
I made one small change, indented the increment on answer, needed to be in loop
Reply
#8
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".
Reply
#9
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  saving issue on homework assignment russoj5 2 1,969 Oct-26-2020, 01:53 PM
Last Post: russoj5
  Need help with iteration homework Dendro 2 2,420 Oct-18-2018, 02:53 AM
Last Post: stullis
  Issue with logic in homework fad3r 5 3,615 May-24-2018, 08:13 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020