类型错误: 参数 od 类型 'int' 在文件 "<string>" 第 31 <module>行上不可迭代


import datetime
import math
import random
options = ("Snorkeling","Scuba Diving","Fishing","Sunbathing","Shopping","Helicopter Ride","Sleeping")
prices  = (10.00, 150.00, 25.00, 0.00, 200.00, 450.00, 0.00)
startDateString = input("Enter the starting date of your vacation (MM/DD/YYYY): ")
startDate = datetime.datetime.strptime(startDateString, '%m/%d/%Y')
stopDateString = input("Enter the ending date of your vacation (MM/DD/YYYY): ")
stopDate = datetime.datetime.strptime(stopDateString, '%m/%d/%Y')
Delta = (stopDate - startDate)
print("Your vacation is", Delta.days, "days long")
costs = []
for i in range(1, 7, Delta.days):
ActivityIndex = random.randrange(len(options))
activity = options in ActivityIndex
cost = prices in ActivityIndex
thisDate = (startDate + datetime.timedelta (days=i))
thisDateString = datetime.datetime.strftime(thisDate, '%m/%d/%y')
print(str.format("On {}, {} costs ${:.2f}",thisDateString,activity,cost))
costs.append(cost)
print(str.format("The most expensive day cost ${}", max(costs)))
print(str.format("The least expensive day cost ${}", min(costs)))

尽管听起来很困惑,但random.randrange返回的是一个整数,而不是一个"随机范围";,正如人们可能认为的那样,在这个代码中:

ActivityIndex = random.randrange(len(options))
activity = options in ActivityIndex
cost = prices in ActivityIndex

ActivityIndex是一个整数,而options in ActivityIndex将尝试对这个单一整数进行迭代,这是不可能的。

相关内容

最新更新