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
将尝试对这个单一整数进行迭代,这是不可能的。