简体   繁体   中英

Check whether a number lies between two other numbers

For some reason my code won't return False EVER and I cant figure it out?

I think the issue is with how my between function is written but it makes sense to me. Also I am struggling to get my restart function to work. If somebody could help me with those 2 areas I would be extremely grateful.

def between(a,b,c):
   if a>b and b<c:
      Rnum =True
   else:
      Rnum=False

def main(): #main function need in all programs for automated testing
    print ("This program will ask the user for 3 numbers and determine if 
    the second number lies betweenthe first and the third")
    print()

    while True:
       numone=input('Please enter the first number - the low number:')
       if numone.isdigit():
           numone=int(numone)
           break
       else:
           print('Invalid response. Please enter a whole number.')


    while True:
       numtwo=input('Please enter the second number - the test number:  ')
       if numtwo.isdigit():
           numtwo=int(numtwo)
           break
       else:
           print('Invalid response. Please enter a whole number.')

    while True:
       numthree=input('Please enter the third number - the high number:')
       if numthree.isdigit():
           numthree=int(numthree)
           break
       else:
            print('Invalid response. Please enter a whole number.')
            sprint()

    number =between(numone,numtwo,numthree)
    print('The statement ' +  str(numone)  + ' lies between '   +  str(numtwo)  +  ' and ' +  str(numthree) + ' is True.'"\n")

    #Restart question
    while True:
        restart = input('Would you like to play again (Y/N)? ')
        if restart == 'Y' or restart == 'y':
            print('Restarting!' + ('\n' * 2))
            break
        if restart == 'N' or restart == 'n':
            print('Thank you for playing.' + ('\n' *2))
            break
        else:
            print("Invalid response. Please answer with a 'Y' or 'N'")
        if restart == 'N' or restart == 'n':
            break
        else:
            continue

if __name__ == '__main__' : 
    main()  #excucte main function

You have small mistake, either in the problem definition or in the example code. Anyways if you modify it a bit:

def between(a,b,c): if b>a and b<c: return 'True'
else: return 'False'

 print('The statement ' +  str(numtwo)  + ' lies between '   
+  str(numone)  +  ' and ' +  str(numthree) + ' is ' +
between(a,b,c) +"\n")

The logic of your between function was slightly wrong (I've rename the variables to make it slightly clearer). In addition, you were not returning the value of the function so it was basically doing nothing. You were also always printing "True".

I have modified your code to return the result of the between function. I have made the result of this function a variable called true_or_false which is then printed at the end of each game.

In order to get your code to loop, all you need is another while loop which you can break out of if the user does not want to continue.

def between(low,test,high):
    if low < test < high:
        return True
    else:
        return False

def main(): #main function need in all programs for automated testing
    print ("This program will ask the user for 3 numbers and determine if\nthe second number lies betweenthe first and the third")

    while True:
        while True:
           numone=input('\nPlease enter the first number - the low number:')
           if numone.isdigit():
               numone=int(numone)
               break
           else:
               print('Invalid response. Please enter a whole number.')

        while True:
           numtwo=input('Please enter the second number - the test number:  ')
           if numtwo.isdigit():
               numtwo=int(numtwo)
               break
           else:
               print('Invalid response. Please enter a whole number.')

        while True:
           numthree=input('Please enter the third number - the high number:')
           if numthree.isdigit():
               numthree=int(numthree)
               break
           else:
                print('Invalid response. Please enter a whole number.')

        true_or_false =between(numone,numtwo,numthree)
        print('The statement ' +  str(numtwo)  + ' lies between '   +  str(numone)  +  ' and ' +  str(numthree) + ' is ' + str(true_or_false) + "\n")

        restart = ""
        while restart.upper() != "Y":

            restart = input('Would you like to play again (Y/N)? ')
            if restart.upper() == "Y":
                print('Restarting!')

            elif restart.upper() == "N":
                print ('Thank you for playing.')
                sys.exit()
            else:
                print("Invalid response. Please answer with a 'Y' or 'N'")


if __name__ == '__main__' :
    main()  #excucte main function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM