在Python中生成随机数时出现属性错误

Too*_*ero 1 python random datetime choice

之前,我曾就同一段代码问过类似的问题,但我再次发现自己陷于困境。特别是在生成包含两个字母,两个数字,然后两个字母的车牌时。我希望这个问题不会重复出现,但是在这种情况下,我非常想做什么,这是到目前为止的代码,希望您能确定我要去哪里:

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")
Run Code Online (Sandbox Code Playgroud)

根据python,问题在于此:

AttributeError: 'builtin_function_or_method' object has no attribute 'choice'
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 5

您没有导入random模块。您导入了random.random() 函数

from random import uniform, random
Run Code Online (Sandbox Code Playgroud)

如果你想使用choice()sample()为好,进口,除了:

from random import uniform, random, choice, sample
Run Code Online (Sandbox Code Playgroud)

并调整您对这些功能的使用:

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))
Run Code Online (Sandbox Code Playgroud)

或者,相反,导入模块

import random
Run Code Online (Sandbox Code Playgroud)

并且您的代码可以正常使用 random(),因为您可以替换uniform()random.uniform()

camInput2 = camInput1 + timedelta(seconds = random.uniform(5, 10))
Run Code Online (Sandbox Code Playgroud)

我再次重申,您不需要创建camInput2,因为它camInput1 + some_timedelta - camInput1产生的值与some_timedelta; 相同;您可以使用:

timeDelta = timedelta(seconds = random.uniform(5, 10))
Run Code Online (Sandbox Code Playgroud)

您从不调用该randomNumbers()函数,该函数也不返回任何内容。该randomNumber功能中的本地名称在该功能之外不可用。

让函数返回结果,并在您现在尝试通过randomNumber名称使用结果的地方使用该函数:

def possibleNumber():
    possibleNumbers = (1,2,3,4,5,6,7,8,9,0)
    return random.sample(possibleNumbers, 2)
Run Code Online (Sandbox Code Playgroud)

licensePlate = str(firstLetters + possibleNumber() + " " + secondLetters)
Run Code Online (Sandbox Code Playgroud)