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
11
  • The code shown is going to give an indentation error. Commented Mar 8, 2017 at 20:11
  • Yeah, please edit your post so that it has the correct indentation as it appears in your code. Commented Mar 8, 2017 at 20:13
  • 1
    This code works fine when properly indented... so your issue is indentation. Commented Mar 8, 2017 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. Commented Mar 8, 2017 at 20:15
  • The code as I see it now runs just fine in Python 3.5 Commented Mar 8, 2017 at 20:26

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

Comments

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

14 Comments

You also removed the inner loop for choosing whether to go again... this is not a functionally equivalent program.
sorry my bad in pasting the code here. Edited@roganjosh
@TemporalWolf Do we need inner loop here ?
Thank you so much for your help! Can you please explain what changes you made so I don't make the same mistake next time? @Bhargav
I removed inner function compare and removed one more while True condition to optimize. Please mark the answer if it worked for u please look for indentation aswell@StacyB. Thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.