我有以下代码:
cust_numb = int(input("Enter number of unique customers then press enter: "))
list_customer = []
for y in range(0,cust_numb):
s = (range(1,cust_numb))
purch_numb = input("How many purchases did customer %s have? " % (s))
list_customer.append(purch_numb)
for z in range(0,cust_numb):
print("Customer %s" % (in range(1,cust_numb)) + "had %s" % (purch_numb) + "purchases.")
当我运行它时,终端会说:
File "main.py", line 8
print("Customer %s" % (in range(1,cust_numb)) + "had %s" %(purch_numb) +
"purchases.")
^
SyntaxError: invalid syntax
我不知道我做错了什么。有帮助吗?
是的,我知道我的某些%s字符串也是错误的。
答案 0 :(得分:0)
代替执行另一个for循环,只需从前一个for循环中获取数字并将其添加一个即可,因此如果您愿意,它从1开始计数。
经测试可正常工作
cust_numb = int(input("Enter number of unique customers then press enter: "))
list_customer = {}
customers = 1
for y in range(0,cust_numb):
purch_numb = int(input("How many purchases did customer %s have? " % (customers)))
list_customer.update({"customer{}".format(customers) : purch_numb})
customers = customers + 1
info = ""
for customer in range(0,cust_numb):
info += "%s " % ("customer{}".format(customer+1)) + "had %s " % (list_customer["customer{}".format(customer+1)]) + "purchases. "
print(info)