0

I'm trying to create a loop in order to bring the user back to the beginning of the program. I can't get it to print "Welcome to the comparison function". The program will run, asking user for input 1 and 2 and then will print the answer of the comparison, but I can not figure out how to get it to start over.

def comparison():
    loop = True
    while (loop):
        print(" Welcome to the comparison function")
    a = int (input("Enter your first number for value a, then hit enter key:"))
    b = int (input("Enter your second number for value b, then hit enter key:"))
    def compare (a,b):
        if a>b:
            return 1
        elif a==b:
            return 0
        else:
            return -1
    answer = compare(a,b)
    print (answer)
    while True:
    response=input ("Would you like to perform another comparison? (yes/no)")
    if response=="yes" or response =="YES" or response =="Yes":
        print ("Thank you!")
        break
    elif response=="no" or response=="NO" or response =="No":
        loop=False
        print ("Thank you, have a great day!")
        break
    else:
        continue
StacyB
  • 13
  • 4
  • The code shown is going to give an indentation error. – Fred Larson Mar 08 '17 at 20:11
  • Yeah, please edit your post so that it has the correct indentation as it appears in your code. – Denziloe Mar 08 '17 at 20:13
  • 1
    This code works fine when properly indented... so your issue is indentation. – TemporalWolf Mar 08 '17 at 20:14
  • Assuming the indentation is fine in your code (it wouldn't run for you at all otherwise), what version of Python are you using? I can get it to run fine in Python 2 but it requires changing `input` to `raw_input`. – roganjosh Mar 08 '17 at 20:15
  • The code as I see it now runs just fine in Python 3.5 – Prune Mar 08 '17 at 20:26
  • By the way, you should remove the definition of **compare** to the top of the program. The way you wrote it, you have to redefine it (to the same value) every time through the loop. – Prune Mar 08 '17 at 20:28
  • Thanks everyone. @Prune, if I remove the def comparison at the top of the program, it just rewrites the first print line repeatedly. Then, if remove that line of print, it works same as before, but doesn't repeat the loop when the user types 'yes'. Did it repeat for you when you ran it? I'm using Python 3.6 – StacyB Mar 08 '17 at 20:58
  • @StacyB Could you fix the indentation in the question so we can see exactly how it is in your program? Get it to the point where you can copy-paste it from the question into a new file and reproduce your issue. – glibdud Mar 08 '17 at 21:07
  • @glibdud it was correct shortly before you posted and for some reason she edited the question to make it incorrect again :( – roganjosh Mar 08 '17 at 21:42
  • @StacyB Why did you remove the indentation fixes? They had it working. Also `if response.lower() == "yes"` covers any case differences. – TemporalWolf Mar 08 '17 at 21:43
  • Sorry, I didn't know that someone else was able to edit the post (my first time posting). I've tried to return it to the way it was before. Like I said, I'm very new at this! I'm learning a lot from this one experiment. Thanks for the help. @Bhargav I copied and pasted the code you had provided and it gave me a syntax error saying "'continue' not properly in loop" – StacyB Mar 08 '17 at 21:54

2 Answers2

1

This would be a drop in replacement for your function in Python 3:

def comparison():
    while True:
        print("Welcome to the comparison function")
        a = int(input("Enter your first number for value a, then hit enter key:"))
        b = int(input("Enter your second number for value b, then hit enter key:"))

        # Recommended replacement for cmp(a, b) in Python 3
        # https://docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons
        answer = (a > b) - (a < b) 
        print(answer)

        response = input("Would you like to perform another comparison? (yes/no)")
        while response.lower() not in ['yes', 'no']:
            response = input("please enter proper response: ")

        if response.lower() == "yes":
            print("Thank you!")
        else:
            print("Thank you, have a great day!")
            break

comparison()
TemporalWolf
  • 7,727
  • 1
  • 30
  • 50
0
def comparision():
    loop = True
    while loop:
            print ("welcome to the comparision function: ")
            a = int(input(("Enter your number for a value a , then hit enter key:")))
            b = int (input("Enter your second number for value b, then hit enter key:"))
            answer = ''
            if a > b:
                answer = 1
            elif a == b:
                answer = 0
            else:
                answer = -1
            print (answer)
            response = str(input("Would you like to perform another comparison? (yes/no) :"))

            while response.lower() not in ['yes', 'no']:
                response = str(input("please enter proper response: "))

            if response.lower() == 'yes'
                continue
            elif response.lower() == 'no' 
                print ("Thank you, have a great day!")
                break

if __name__ == '__main__':
    comparision()

if you are using python 2.7:

response = str(raw_input("Would you like to perform another comparison? (yes/no) :"))

Hope this helps. Thanks

Bhargav
  • 454
  • 1
  • 6
  • 15