如何修复Python3中的'NameError:name'random'is not defined'错误

时间:2019-01-04 00:52:57

标签: python

前一段时间我用Python 3编写了一个程序,并且由于对在单独的Python 3程序中运行该程序感兴趣(我曾使用exec(open("filename.py").read())来做到这一点)。

但是,在运行原始程序的单独程序中,它给出了以下错误消息:NameError: name 'random' is not defined。我肯定已经导入了random,所以我看不到为什么它会给我这个信息。我认为这些应该是程序中的重要内容:

#!/usr/bin/env python3

import random, time  # the top lines of the program

if random.randint(1, 100) >= 36:  # the line which the error message points me to
    ... 

老实说,我不知道我在这里能做什么-任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

使代码生效,然后在代码末尾使用create-react-app

public class main extends AppCompatActivity {

private List Service_UUIDs;
private List Charac_UUIDS;
private List Descriptor_UUIDs;
public static final UUID descriptor = UUID.fromString("863930fc-947c-421b- 
9170-9969ea6ad610");
private BluetoothAdapter bluetoothAdapter = null;
private BluetoothLeScanner bluetoothLeScanner;
private ScanCallback scanCallback;
private TextView messageText;
private Handler handler;
private static final int SCAN_TIME = 15000;
private int connectionState = STATE_DISCONNECTED;
private static final int STATE_DISCONNECTED = 0;
private static final int STATE_CONNECTED = 2;
private boolean isConnected;
private boolean isScanning;
private BluetoothLeScanner myScanner;
private BluetoothGatt bluetoothGatt;
private String connected = "";
public List<BluetoothGattService> myServices;
private ScanCallback myScanCallBack;
private BluetoothDevice myDevice;
private HashMap<String, BluetoothDevice> myScanResults;
public final static String ACTION_GATT_SERVICES_DISCOVERED =
        "com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";
public final static String ACTION_DATA_AVAILABLE =
        "com.example.bluetooth.le.ACTION_DATA_AVAILABLE";

public static final int REQUEST_ENABLE_BT = 10;

private void unpairDevice(BluetoothDevice device) {
    try {
        Method m = device.getClass().getMethod("removeBond", (Class[]) 
null);
        m.invoke(device, (Object[]) null);
        Log.d("unpairDevice", Integer.toString(device.getBondState()));
    }
    catch (Exception e) { Log.e("unpairDevice", e.getMessage()); }
}

private void makeToast(String message) {
    //creates pop-ups in UI of the message
    Context context = getApplicationContext();
    CharSequence text = message;
    int duration = Toast.LENGTH_LONG;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
}

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BluetoothManager bluetoothManager = (BluetoothManager) 
getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();

        if(!bluetoothManager.getAdapter().isEnabled()){
            Intent btEnable = new 
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(btEnable, REQUEST_ENABLE_BT);
        }
    messageText = findViewById(R.id.atm_msg);

    findViewById(R.id.scan_button).setOnClickListener(new handleScan());
}

class handleScan implements View.OnClickListener{
    public void onClick(View view){
        startScan();
    }
}

private void startScan() {
    List<ScanFilter> filters = new ArrayList<>();
    ScanFilter scanFilter = new ScanFilter.Builder()
            .setServiceUuid(new ParcelUuid(descriptor))
            .build();
    filters.add(scanFilter);
    ScanSettings settings = new ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
            .build();
    myScanResults = new HashMap<>();
    myScanCallBack = new BtleScanCallback(myScanResults);
    bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
    bluetoothLeScanner.startScan(myScanCallBack);
    isScanning = true;
    new Handler().postDelayed(this::stopScan, SCAN_TIME);
}

private void stopScan(){
    if(isScanning && bluetoothAdapter != null
            && bluetoothAdapter.isEnabled()
            && bluetoothLeScanner != null){
        bluetoothLeScanner.stopScan(myScanCallBack);
        scanComplete();
    }

    myScanCallBack = null;
    isScanning = false;
    handler = null;
}

private void scanComplete(){
    if (myScanResults.isEmpty()){
        return;
    }

    for(BluetoothDevice device: myScanResults.values()){
        connectDevice(device);
    }
}

protected class BtleScanCallback extends ScanCallback {

    private HashMap<String, BluetoothDevice> myScanResults;

    BtleScanCallback(HashMap<String, BluetoothDevice> scanResults) {
        myScanResults = scanResults;
    }

    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        addScanResult(result);
    }

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        for (ScanResult result : results) {
            addScanResult(result);
        }
    }

    @Override
    public void onScanFailed(int errorCode) {
        makeToast("BLE Scan Failed with code " + errorCode);
    }

    private void addScanResult(ScanResult result) {
        myDevice = result.getDevice();
        String deviceAddress = myDevice.getAddress();
        myScanResults.put(deviceAddress, myDevice);
    }
}

private void connectDevice(BluetoothDevice device) {
    bluetoothGatt = device.connectGatt(this, false, bluetoothGattCallback);
}

private final BluetoothGattCallback bluetoothGattCallback = new 
BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int 
newState) {
        super.onConnectionStateChange(gatt, status, newState);
        if (status == gatt.GATT_FAILURE){
            Log.d("BLE_Assistance", "-----> status: " + gatt.GATT_FAILURE);
            disconnectGattServer();
            return;
        }

        if (status == 133){
            bluetoothAdapter.disable();
            bluetoothAdapter.enable();
            Log.d("OCSC: ", "133 GATT_ERROR OCCURRED");
        }
        if (newState == gatt.STATE_CONNECTED){
            Log.d("BLE_Assistance", "-----> state: " + 
gatt.STATE_CONNECTED);
            connectionState = gatt.STATE_CONNECTED;
            isConnected = true;
            return;
        }
        else if (status != gatt.GATT_SUCCESS){
            Log.d("BLE_Assistance", "-----> status: could not connect");
            disconnectGattServer();
            return;
        }
        else if (newState == gatt.STATE_DISCONNECTED){
            Log.d("BLE_Assistance", "status: " + gatt.STATE_DISCONNECTED);
            disconnectGattServer();
        }
    }

    public void disconnectGattServer(){
        isConnected = false;
        connected = "";
        if (bluetoothLeScanner != null && scanCallback != null){
            bluetoothLeScanner.flushPendingScanResults(scanCallback);
        }
        if (!myScanResults.isEmpty()){
            myScanResults.clear();
        }

        try {
            if (myServices.isEmpty()){
                Log.d("Services", "Empty");
            }
            else{
                myServices.clear();
            }
        } catch (NullPointerException nullPointerException){
            Log.d("Services", "" + nullPointerException);
        }

        bluetoothAdapter.cancelDiscovery();

        if(bluetoothGatt != null){
            bluetoothGatt.abortReliableWrite();
            bluetoothGatt.disconnect();
            bluetoothGatt.close();
            bluetoothGatt = null;
            Log.d("disconnect server", ": Disconnected GATT Server");
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        byte[] msg_bytes = characteristic.getValue();

        try {
            String message = new String(msg_bytes, 0, msg_bytes.length, 
 "utf-8");
            Log.d("onCharacteristicRead", message);

            new Handler(Looper.getMainLooper()).postDelayed(() -> {
                makeToast("onCharacteristicRead: " + message);
                messageText.append("\nonCharacteristicRead: " + message);
            }, 1000);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status){
        super.onServicesDiscovered(gatt,status);
        Service_UUIDs = new ArrayList();
        Charac_UUIDS = new ArrayList();
        Descriptor_UUIDs = new ArrayList();
        if(status != gatt.GATT_SUCCESS){
            return;
        }else{
            myServices = gatt.getServices();
            for (BluetoothGattService service : myServices) {
                for (BluetoothGattCharacteristic charac : service.getCharacteristics()) {
                    if (charac.getUuid().toString().contains("863930fc")) {
                        Service_UUIDs.add(charac.getService().getUuid().toString());
                        Charac_UUIDS.add(charac.getUuid().toString());
                        Descriptor_UUIDs.add(descriptor);
                        BluetoothGattDescriptor desc = charac.getDescriptor(descriptor);
                        gatt.setCharacteristicNotification(charac, true);
                        desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                        gatt.writeDescriptor(desc);
                    }
                }
            }
            Log.d("BLE_Assistance", "------ UUID Lists: " + Service_UUIDs + Charac_UUIDS + Descriptor_UUIDs);
        }

    }
};