JiggityJ
JiggityJ

Reputation: 21

Keep receiving a nonetype error

I'm still super new at this. The code is supposed to continue looping until/unless I hit enter without entering a value. It goes for 2 loops, and then gives me a NoneType error. Frustrated. Please help. My code:

import random
hourly = 70
CustomerID = random.randint(100000,999999)
Customers = {}
characters = ['a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z']

def generateRecord():
    cusName = input('Enter customer name:')
    cusIncome = int(input('Enter customer annual income:'))
    consulTime = int(input('Enter Consultation time in minutes:'))
    if cusIncome <= 25000:
                     CustRate = (.4)
                     print('Rate = 40% for time over 30 minutes')
    if cusIncome > 25000:
                     CustRate = (.7)
                     print('Rate = 70% for time over 20 minutes')
    if CustRate ==.4 and consulTime< 30:
        CustRate == 0
    if CustRate==.7 and consulTime <20:
        CustRate == 0

    billingAmt = hourly*CustRate*(consulTime/60)
    CustomerID
    print('Customer ID is '+str(CustomerID))
    Customers[CustomerID] = [cusName, cusIncome, consulTime, CustRate, billingAmt]

def generateBillAmount():
    cusIncome = int(input('Enter customer annual income:'))
    consulTime = int(input('Enter Consultation time in minutes:'))
    if cusIncome <= 25000:
        CustRate = .4
        print('Rate = 40% for time over 30 minutes')
    else:
        CustRate = .7
        print('Rate = 70% for time over 20 minutes')
    if CustRate ==.4 and consulTime< 30:
        CustRate = 0
    if CustRate==.7 and consulTime <20:
        CustRate = 0
    billingAmt = int(hourly*CustRate*(consulTime/60))
    if CustRate==.4 and consulTime>30:
        billingAmt = int(hourly*CustRate*((consulTime-30)/60))
    if CustRate==.7 and consulTime>20:
        billingAmt = int(hourly*CustRate*((consulTime-20)/60))
    print('Customer total is ' +str(billingAmt)+' dollars.')

generateRecord()
for cusName in generateRecord():
    for character in cusName:
        if cusName == '\n':
            print(Customers)
            break
        else:
            generateRecord()
generateBillAmount()

Here's what it returns, usually:

Enter customer name:jake
Enter customer annual income:15000
Enter Consultation time in minutes:15
Rate = 40% for time over 30 minutes
Customer ID is 594578
Enter customer name:trix
Enter customer annual income:45000
Enter Consultation time in minutes:45
Rate = 70% for time over 20 minutes
Customer ID is 594578

Traceback (most recent call last):
  File "C:/Users/Hammad/GenerateRecords.py", line 45, in <module>
    *for cusName in generateRecord():
TypeError: 'NoneType' object is not iterable*

Upvotes: 2

Views: 311

Answers (2)

A. N. Other
A. N. Other

Reputation: 407

It is because generateRecord() does not return anything. Therefore anything created will not be saved. So by default it will be None which cannot be iterated over. Change the definition to the following

def generateRecord(Customers):

Add at the end of that function add

return Customers

This will then change the Customers dictionary outside the scope of the function...

Upvotes: 1

Mayank
Mayank

Reputation: 5569

Unless specified explicitly a return value, python returns None as default. Since you are looping over the response of generateRecord() which is None; so you are getting an error. Try defining a return value for the function

Upvotes: 2

Related Questions