在Python中生成随机数时出现属性错误 [英] Attribute error when generating random numbers in Python

查看:436
本文介绍了在Python中生成随机数时出现属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I asked a similar question regarding this same piece of code earlier but once again I have found myself stuck. Particularly on the generation of a license plate containing two letters, two numbers, and then two letters. I hope that this question isn't a duplicate but in this circumstance I am very stuck with what to do, this is the code so far and I hope you can identify where I am going wrong:

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,问题在于:

According to python, the issue is to do with this:

AttributeError: 'builtin_function_or_method' object has no attribute 'choice'


推荐答案

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

You did not import the random module. You imported the random.random() function:

from random import uniform, random

如果要使用 choice() sample()以及另外导入的内容:

If you wanted to use choice() and sample() as well, import that in addition:

from random import uniform, random, choice, sample

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

and adjust your use of those functions:

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))

或者导入 just模块

import random

,并且您的代码将正常运行,因为您实际上并未在任何地方使用 random()您将 uniform()替换为 random.uniform()

and your code will work as you don't actually use random() anywhere, provided you replace uniform() with random.uniform():

camInput2 = camInput1 + timedelta(seconds = random.uniform(5, 10))

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

I'll reiterate again that you don't need to create camInput2, as camInput1 + some_timedelta - camInput1 produces the same value as some_timedelta; you can just use:

timeDelta = timedelta(seconds = random.uniform(5, 10))

您永远不会调用 randomNumbers()函数,而函数返回任何东西。该函数中的 randomNumber 本地名称在该函数之外不可用。

You never call the randomNumbers() function, nor does the function return anything. The randomNumber local name in that function is not available outside of the function.

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

Have the function return the result and use the function where you now try to use the result via the randomNumber name:

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

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

这篇关于在Python中生成随机数时出现属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆