Python中生成随机数时的属性错误

Attribute error when generating random numbers in Python

我之前就同一段代码问过一个类似的问题,但我又一次发现自己被卡住了。特别是在生成包含两个字母、两个数字和两个字母的车牌时。我希望这个问题不是重复的,但在这种情况下我很困惑该怎么做,这是到目前为止的代码,我希望你能确定我哪里出错了:

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'

您没有导入 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() 函数,函数 return 也不会调用任何东西。该函数中的 randomNumber 本地名称在函数外部不可用。

具有函数 return 结果并使用该函数,您现在尝试通过 randomNumber 名称使用结果:

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

licensePlate = str(firstLetters + possibleNumber() + " " + secondLetters)

random.random,您在代码中将其简称为 random,因为您将其直接导入到命名空间 (from random import ... random) 而不是导入整个模块 (import random), 是一个自由函数,它没有名为 choice.

的属性

对于您编写的代码,调用 random.choicerandom.sample,您的导入语句应该是 import random。或者,将您的函数调用切换为简单的 choice(...)sample(...)...

有一天我的代码因为同样的错误而停止工作时,我遇到了同样的问题。 阅读其他答案我想出了这个解决方法: 给random模块起一个别名,这样当你调用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))