我早些时候就同一段代码问了一个类似的问题,但我又一次发现自己陷入困境。特别是在生成包含两个字母,两个数字,然后是两个字母的车牌上。我希望这个问题不是重复的,但在这种情况下,我很不知所措,到目前为止这是代码,我希望你能找出我出错的地方:
from datetime import date, datetime, time, timedelta
import time, string
from random import uniform, random
def timeDelta():
print("Average Speed Checker")
start = (input("Car has passed Cam1: "))
licensePlate = str(firstLetters + randomNumber + " " + secondLetters)
print(licensePlate)
if start in ("y"):
camInput1 = datetime.now()
print(camInput1)
print("Car is travelling...")
time.sleep(1)
print("Car is travelling...")
time.sleep(1)
print("Car has passed cam2")
camInput2 = camInput1 + timedelta(seconds = uniform(5, 10))
timeDelta = camInput2 - camInput1
distance = 200
duration = timeDelta.total_seconds()
print("Time Delta is equal to: {0}".format(duration))
speedCarMs = distance/duration
print("Car is travelling in m/s at: {0}".format(speedCarMs))
speedCarMph = 2.237*speedCarMs
print("Car is travelling in MPH at: {0}".format(speedCarMph))
if speedCarMph > 60:
fileInput:
def possibleNumber():
possibleNumbers = (1,2,3,4,5,6,7,8,9,0)
randomNumber = random.sample(possibleNumbers, 2)
def randomLetters1(y):
return ''.join(random.choice(string.ascii_uppercase) for x in range(y))
firstLetters = (randomLetters1(2))
secondLetters = (randomLetters1(3))
print("Choose which function you want to use: ")
while True:
answer = (input("Choice: "))
if answer in ("speed"):
timeDelta()
else:
print("Invalid response")
根据python,问题与此有关:
AttributeError: 'builtin_function_or_method' object has no attribute 'choice'
答案 0 :(得分:4)
您没有导入random
模块。您导入了random.random()
功能:
from random import uniform, random
如果您还想使用choice()
和sample()
,请另外导入:
from random import uniform, random, choice, sample
并调整您对这些功能的使用:
def possibleNumber():
possibleNumbers = (1,2,3,4,5,6,7,8,9,0)
randomNumber = sample(possibleNumbers, 2)
def randomLetters1(y):
return ''.join(choice(string.ascii_uppercase) for x in range(y))
或者,只需导入模块:
import random
如果您将random()
替换为uniform()
,那么您的代码将无法在任何地方实际 random.uniform()
使用:
camInput2 = camInput1 + timedelta(seconds = random.uniform(5, 10))
我再次重申,您不需要创建camInput2
,因为camInput1 + some_timedelta - camInput1
生成的值与some_timedelta
相同;你可以使用:
timeDelta = timedelta(seconds = random.uniform(5, 10))
您永远不会调用randomNumbers()
函数,函数也不会返回。该函数中的randomNumber
本地名称在函数外部不可用。
让函数返回结果并使用现在尝试通过randomNumber
名称使用结果的函数:
def possibleNumber():
possibleNumbers = (1,2,3,4,5,6,7,8,9,0)
return random.sample(possibleNumbers, 2)
和
licensePlate = str(firstLetters + possibleNumber() + " " + secondLetters)
答案 1 :(得分:1)
random.random
,您在代码中仅称为random
,因为您将其直接导入您的命名空间(from random import ... random
),而不是导入整个模块(import random
}),是一个自由函数,没有名为choice
的属性。
对于您编写的代码,调用random.choice
和random.sample
,您的import语句应为import random
。或者,将您的函数调用切换为choice(...)
和sample(...)
...
答案 2 :(得分:1)
有一天我的代码由于相同的错误而停止工作时,遇到了相同的问题。 阅读其他答案,我想出了以下解决方法: 为随机模块提供一个别名,以便在调用alias.choice方法时不会产生歧义:
import json,random as rd, string
from random import uniform, random, choice, sample
def randomString(stringLength):
letters = string.ascii_letters
return ''.join(rd.choice(letters) for i in range(stringLength))