我无法弄清楚代码中的IndexError在哪里,所以有人可以帮我解决
这是代码
def gap(g, m, n):
def is_prime(n):
for i in range(2, n):
if n % i == 0:
return False
return True
result = []
r = []
primes = [p for p in range(m, n) if is_prime(p)]
for i in range(len(primes) - 1):
if primes[i] - primes[i + 1] == g:
r.append(primes[i - 1])
r.append(primes[i])
result.append(r[0])
result.append(r[1])
if result == []:
return None
else:
return result
print (gap(2, 100, 110))
这是输出
Traceback (most recent call last):
File "C:\Users\Harry\Documents\randompythonprograms\editable.py", line 20, in <module>
print (gap(2, 100, 110))
File "C:\Users\Harry\Documents\randompythonprograms\editable.py", line 14, in gap
result.append(r[0])
IndexError: list index out of range
答案 0 :(得分:0)
这是错误的原因。
r.append(primes[i - 1])
我需要i作为列表的第一个值id并传递(i - 1)。
答案 1 :(得分:0)
#include "application.h"
SYSTEM_MODE(MANUAL);
// TCP setup
TCPServer server = TCPServer(23);
TCPClient client;
int range;
// char read;
String readString;
String sentRange;
// LIDAR sensor setup
const int monitorPin1 = 3;
const int monitorPin2 = 1;
const int triggerPin1 = 2;
const int triggerPin2 = 0;
long pulseWidth;
// Initialise averaging array
const int numberOfReadings = 10; // reasonable DOF from Measurement and Noise
const int med = numberOfReadings/2;
long readings[numberOfReadings];
int median;
void setup() {
Serial.begin(9600);
WiFi.clearCredentials();
//LIDAR
pinMode(triggerPin1, OUTPUT); // Set pin 2 as trigger pin
pinMode(D7, OUTPUT);
digitalWrite(triggerPin1, LOW); // Set trigger HIGH for non-continuous read
pinMode(monitorPin1, INPUT); // Set pin 3 as monitor pin
pinMode(triggerPin2, OUTPUT); // Set pin 2 as trigger pin
digitalWrite(triggerPin2, LOW); // Set trigger HIGH for non-continuous read
pinMode(monitorPin2, INPUT); // Set pin 3 as monitor pin
delay(2000);
WiFi.on(); // if this isn't working, make sure it's set up for Photons (not Electrons!)
WiFi.setCredentials("AndroidAP", "12345678"); // NB: must call WiFi.on() before this line. Not sure why.
delay(2000);
WiFi.connect();
// start listening for clients
server.begin();
}
int readLIDAR(int triggerPin, int monitorPin){
pulseWidth = pulseIn(monitorPin, HIGH); // Count how long the pulse is high in microseconds
pulseWidth = pulseWidth / 10; // 10usec = 1 cm of distance
return pulseWidth;
}
long takeReading(int triggerPin, int monitorPin) {
for (int i = 0; i < numberOfReadings; i++) {
readings[i] = readLIDAR(triggerPin, monitorPin);
delay(10); // There doesn't seem to be a requirement for a delay, but to be safe, delay = 10ms.
}
return *readings;
}
// Sort readings
long sort(long* readings) {
// bubble sort with flag
for (int i = 0; i < numberOfReadings; i++) {
bool flag = true;
for (int j = 1; j < numberOfReadings; j++) {
if (readings[j-1] > readings[j]) {
int t = readings[j-1];
readings[j-1] = readings[j];
readings[j] = t;
flag = false;
}
}
if (flag) break;
}
return *readings;
}
int range1(String command){
if (command == "Get range 1\n") *readings = takeReading(triggerPin1, monitorPin1); // gather sufficient (typ: 10) readings
else if (command == "Get range 2\n") *readings = takeReading(triggerPin2, monitorPin2); // gather sufficient (typ: 10) readings
*readings = sort(readings); // sort in ascending order for median value
median = (readings[med-1] + readings[med])/2; // get median value to send to app
memset(readings, 0, sizeof(readings)); //clear previous readings
return median;
}
String goGetCredentials() {
WiFiAccessPoint ap[5];
int found = WiFi.getCredentials(ap, 5);
for (int i = 0; i < found; i++) {
Serial.print("ssid: ");
Serial.println(ap[i].ssid);
// security is one of WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA, WLAN_SEC_WPA2
Serial.print("security: ");
Serial.println(ap[i].security);
// cipher is one of WLAN_CIPHER_AES, WLAN_CIPHER_TKIP
Serial.print("cipher: ");
Serial.println(ap[i].cipher);
}
}
void loop() {
if (client.connected()) {
Serial.println("Client connected.");
// read client for which sensor to query and send back result
while (client.available()) {
readString = "Get range 1\n";
range = range1(readString);
sentRange = "u" + String(range) + " cm" + "\n";
Serial.print(sentRange);
server.write(sentRange);
delay(300);
readString = "Get range 2\n";
range = range1(readString);
sentRange = "f" + String(range) + " cm" + "\n";
Serial.print(sentRange);
server.write(sentRange);
delay(300);
}
} else {
// if no client is yet connected, check for a new connection
client = server.available();
if(WiFi.hasCredentials()==true) {
Serial.println("Has credentials.");
// goGetCredentials();
} else {
Serial.println("No credentials.");
}
Serial.println("Looking for server.");
delay(2000);
}
}
您已将r定义为空数组,并且在循环后仍为空数组。您可以通过添加简单的print语句来检查它。所以它是一个没有索引0或1的数组,所以它抛出了那个错误。