我还是个新手。代码应该继续循环,直到/除非我在没有输入值的情况下按enter键。它运行两个循环,然后给我一个NoneType
错误。沮丧的。请帮忙。我的代码:
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()
它通常会返回以下内容:
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*
这是因为
generateRecord()
不返回任何内容。因此,创建的任何内容都不会被保存。因此,默认情况下它将是None
,不能迭代。将定义更改为以下内容在函数的末尾添加Add
这将在函数范围之外更改Customers字典。。。你知道吗
除非显式指定
return
值,否则python返回None
作为默认值。因为您在generateRecord()
的响应上循环,这就是None
;所以您得到了一个错误。尝试定义函数的返回值相关问题 更多 >
编程相关推荐