if语句返回时循环的Python脚本!= True

时间:2017-05-16 22:19:34

标签: python python-3.x

如果第二个函数没有返回False并且似乎遇到问题,我试图让这个代码重复。

此外,第二个函数中的if语句不接受我应该传递的整数(即当我通过1-8时)。也许range()不合适吗?

def askanything():
    choice = (int(input("Which battery? (1-8):")))
    batchoice(choice)

def batchoice(bat):
    if bat in range(1-9):
        return True
    else:
        print("sorry, selection must be between 1-8")
        askanything()

1 个答案:

答案 0 :(得分:0)

您需要一个while循环才能继续询问问题,直到满足条件。你不想在函数调用中调用函数。简化这一点的一种方法是将它全部放在一个函数中:

def askanything():
    done = False
    while not done:
        choice = (int(input("Which battery? (1-8):")))
        if(choice in range(1,9)):
            done = True
        else:
            print('Sorry, selection must be between 1-8')