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

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

问题描述:

我之前问过一个类似的问题,但之前我发现自己陷入了困境。特别是在生成包含两个字母,两个数字和两个字母的车牌时。我希望这个问题不是一个重复,但在这种情况下,我很坚持了怎么办,这是迄今为止的代码,我希望你能找出我要去的地方错了:在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") 

根据蟒蛇,问题是这样做的:

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()的任何地方,只要你有random.uniform()替换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) 
+0

我没有使用'uniform()'但是,谢谢你的回应,现在我被告知'randomNumber'没有被定义? – TooLateTheHero 2015-02-05 21:04:36

+0

@TooLateTheHero:'randomNumber'只是'possibleNumber()'函数中的一个局部变量,而不是全局变量。它不在函数之外。为什么不使用'return'并存储结果? – 2015-02-05 21:08:14

+0

对不起,打扰你,但我不完全熟悉'return'的使用,你能帮我把它放到位吗?我明白你的意思,我不明白我会如何将它放入我的代码中,谢谢。 – TooLateTheHero 2015-02-05 21:16:06

random.random,你指的是你的代码只是random,因为您直接将它导入到您的名称空间(from random import ... random)而不是导入整个模块(import random),它是一个免费函数,它没有属性名为choice

对于您所写的代码,请致电random.choicerandom.sample,您的进口声明应为import random。或者,将您的函数调用切换为choice(...)sample(...) ...