[英]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?出于某种原因,我的代码永远不会返回 False 而我无法弄清楚?
I think the issue is with how my between
function is written but it makes sense to me.我认为问题在于我的
between
函数是如何编写的,但这对我来说很有意义。 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).你的
between
函数的逻辑有点错误(我重命名了变量以使其更清晰)。 In addition, you were not returning
the value of the function so it was basically doing nothing.此外,您没有
returning
函数的值,因此它基本上什么都不做。 You were also always printing "True".你也总是打印“True”。
I have modified your code to return
the result of the between
function.我已经修改了您的代码以
return
between
函数的结果。 I have made the result of this function a variable called true_or_false
which is then printed at the end of each game.我已经将这个函数的结果变成了一个名为
true_or_false
的变量,然后在每场比赛结束时打印出来。
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.为了让您的代码循环,您只需要另一个
while
循环,如果用户不想继续,您可以中断该循环。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.