验证错误

时间:2016-02-28 17:38:20

标签: python python-3.x

我刚刚开始编码并且我已经遇到了错误,我尝试了不同的方法,但它不起作用,有人可以告诉我导致错误的原因是什么吗?

itemsneeded=''
while not itemsneeded.isnumeric() and int(itemsneeded)<5:
    itemsneeded=(input('How many items do you need?'))

错误:带有基数10的int()的文字无效:&#34;

2 个答案:

答案 0 :(得分:1)

我建议在满足所有要求时“无限地”循环并打破循环:

while True:
    itemsneeded = input("How many items do you need?")
    if itemsneeded.isnumeric() and int(itemsneeded) < 5:
        break

答案 1 :(得分:0)

试试这个:

while True:
    try:    
        itemsneeded = int(input("How many items do you need?"))
    except ValueError: 
        print("Not an number")
        break
    if int(itemsneeded) < 5:
        break