为什么我收到Python的类型错误?

时间:2019-10-13 20:29:38

标签: python

“编写一个程序,将总找零金额作为一个整数输入,该程序使用最少的硬币(每行一种硬币类型)输出找零。硬币类型为美元,四分之一,角钱,镍币和几美分。使用单数和复数硬币适当的名称,例如1便士vs. 2便士。您的程序必须定义并调用以下函数。函数exact_change()应该返回num_dollars,num_quarters,num_dimes,num_nickels和num_pennies。

“ num_dollars,num_quarters,num_dimes,num_nickels,num_pennies =确切_change(input_val)”行给我带来了麻烦,但它是作业中的必填行。

错误提示: 在第36行的文件“ main.py”中 num_dollars,num_quarters,num_dimes,num_nickels,num_pennies =确切变化(input_val) TypeError:“ NoneType”对象不可迭代

if __name__ == '__main__': 
    input_val = int(input())

num_dollars, num_quarters, num_dimes, num_nickels, num_pennies = exact_change(input_val)

    num_dollars = input_val // 100
    input_val %= 100
    num_quarters = input_val // 25
    input_val %= 25
    num_dimes = input_val // 10 
    input_val %= 10
    num_nickels = input_val // 5
    input_val %= 5
    num_pennies = input_val

    if num_dollars >1:
        print('%d dollars' % num_dollars)
    elif num_dollars ==1:
        print('%d dollar' % num_dollars)
    if num_quarters > 1:
        print('%d quarters' % num_quarters)
    elif num_quarters ==1:
        print('%d quarter' % num_quarters)
    if num_dimes >1:
        print('%d dimes' % num_dimes)
    elif num_dimes ==1:
        print('%d dime' % num_dimes)
    if num_nickels >1:
        print('%d nickels' % num_nickels)
    elif num_nickels ==1:
        print('%d nickel' % num_nickels)
    if num_pennies >1:
        print('%d pennies' % num_pennies)
    elif num_pennies ==1:
        print('%d penny' % num_pennies)
if input_val <= 0:
    print('no change')

错误提示:

文件“ main.py”,第36行,在 num_dollars,num_quarters,num_dimes,num_nickels,num_pennies =确切变化(input_val) TypeError:“ NoneType”对象不可迭代

3 个答案:

答案 0 :(得分:0)

它应该看起来像这样

/

答案 1 :(得分:0)

onInputValueChange: ({ inputValue }) => {
  setInputItems(
    items.filter((item) => item.Description.toLowerCase().startsWith(inputValue.toLowerCase()))
  );
},

答案 2 :(得分:0)

我目前也在做Zybooks的Python编码。这是代码的外观,以便通过所有程序的测试。

#define exact change function
def exact_change(user_total):
    num_dollars = user_total // 100 #convert to dollars
    user_total %= 100 #get remainder after conversion
    num_quarters = user_total // 25 #convert to quarters
    user_total %= 25 #get remainder after conversion
    num_dimes = user_total // 10 #convert to dimes
    user_total %= 10 #get remainder after conversion
    num_nickels = user_total // 5 #convert to nickels
    user_total %= 5 #get remainder after conversion
    num_pennies = user_total
    return(num_dollars, num_quarters, num_dimes, num_nickels, num_pennies)
if __name__ == '__main__':
    input_val = int(input()) #prompt user to input an integer
    num_dollars, num_quarters, num_dimes, num_nickels, num_pennies = exact_change(input_val) #recall exact_change function
    
    #define output statements to output number of exact_change variables: 
    #num_dollars, num_quarters, num_dimes, num_nickels, num_pennies  
    if input_val <=0: #if amount is zero
        print('no change') #print output
        
    else:
        if num_dollars > 1: #if number of dollars is greater than one
            print('%d dollars' % num_dollars) #print number of dollars
        elif num_dollars == 1: # if number of dollars equal 1
            print('%d dollar' % num_dollars) #print dollar in singular
        
        if num_quarters > 1: #if number of quarters is greater than one
            print('%d quarters' % num_quarters) #print number of quarters
        elif num_quarters ==1: # if number of quarters equal 1
            print('%d quarter' % num_quarters) #print quarter in singular
        
        if num_dimes > 1: #if number of dimes is greater than one
            print('%d dimes' % num_dimes) #print number of dimes
        elif num_dimes == 1: # if number of dimes equal 1
            print('%d dime' % num_dimes) #print dime in singular
            
        if num_nickels > 1: #if number of nickels is greater than one
            print('%d nickels' % num_nickels) #print number of nickels
        elif num_nickels == 1: # if number of nickels equal 1
            print('%d nickel' % num_nickels) #print nickel in singular
        
        if num_pennies >1: #if number pennies is greater than one
            print('%d pennies' % num_pennies) #print number of pennies
        elif num_pennies ==1: # if number of pennies equal 1
            print('%d penny' % num_pennies) #print penny in singular