Python 3.x 我想用这个函数验证用户输入,但即使在满足正确的参数后,循环仍会重复

Python 3.x 我想用这个函数验证用户输入,但即使在满足正确的参数后,循环仍会重复,python-3.x,Python 3.x,此函数用于验证用户输入 height = int(input('Enter an odd number 5 or greater: ')) while True: if height % 2 == 0 and height < 5: int(input("Error! Try again ")) #This function is used to validate the user input

此函数用于验证用户输入

    height = int(input('Enter an odd number 5 or greater: '))
    while True:
        if height % 2 == 0 and height < 5:
            int(input("Error! Try again "))    #This function is used to validate the user input
    
 
        elif (height % 2) != 0 and height >= 5:
            print(height) 
height=int(输入('输入奇数5或更大:'))
尽管如此:
如果高度%2==0且高度<5:
int(输入(“错误!重试”)#此函数用于验证用户输入
elif(高度%2)!=0和高度>=5:
打印(高度)

而不是使用
elif(高度%2)!=0和高度>=5时,可以使用清洁器
if-else
子句

    height = int(input('Enter an odd number 5 or greater: '))
    while True:
        if (height % 2) != 0 and height >= 5:
            print(height)
            break   #to exit the loop

        else :
            height = int(input("Error! Try again "))    

print
语句后添加
break
,谢谢您的帮助,但是如果用户输入错误,比如说2次,则会出现这种情况。但如果回答正确,函数仍会打印错误消息。知道为什么吗?更新了答案。您没有将输入存储在
height
变量中