Related
I have a long line of code that prints a bunch of symbols for graphics, and I just started getting an index out of range error. it is inside a function, and this the code that matters:
at top, after imports(fr termcolor cprint, colored , time, os, random)
rannum = random.randrange(1,20,1)
the function:
def obstacle():
rocknum = random.randrange(0,12,1)
rock = ["on_cyan","on_cyan","on_cyan","on_cyan","on_cyan","on_cyan","on_cyan","on_cyan","on_cyan","on_cyan","on_cyan","on_cyan"]
rock[rocknum]= "on_cyan"
ongroundls = [" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "] #runframe 1 list
pos = rannum
#print(rannum)
if rannum == random.randrange(0,20,1):
#print(rannum, pos)
ongroundls[rannum] = "4"
rock[rocknum] = "on_yellow"
else:
rock[rocknum] = "on_cyan"
#print(colored(ongroundls[0],"grey","on_cyan")+colored(ongroundls[1],"grey","on_cyan")+colored(ongroundls[2],"grey","on_cyan")+colored(ongroundls[3],"grey","on_cyan")+colored(ongroundls[4],"grey","on_cyan")+colored(ongroundls[5],"grey","on_cyan")+colored(ongroundls[6],"grey","on_cyan")+colored(ongroundls[7],"grey","on_cyan")+colored(ongroundls[8],"grey","on_cyan")+colored(ongroundls[9],"grey","on_cyan")+colored(ongroundls[10],"grey","on_cyan")+colored(ongroundls[11],"grey","on_cyan")+colored(ongroundls[12],"grey","on_cyan")+colored(ongroundls[13],"grey","on_cyan")+colored(ongroundls[14],"grey","on_cyan")+colored(ongroundls[15],"grey","on_cyan")+colored(ongroundls[16],"grey","on_cyan")+colored(ongroundls[17],"grey","on_cyan")+colored(ongroundls[18],"grey","on_cyan")+colored(ongroundls[19],"grey",rock[0])+colored(ongroundls[20],"grey",rock[1])+colored(ongroundls[21],"grey",rock[2])+colored(ongroundls[22],"grey",rock[3])+colored(ongroundls[23],"grey",rock[4])+colored(ongroundls[24],"grey",rock[5])+colored(ongroundls[25],"grey",rock[6])+colored(ongroundls[26],"grey",rock[7])+colored(ongroundls[27],"grey",rock[8])+colored(ongroundls[28],"grey",rock[9])+colored(ongroundls[29],"grey",rock[10])+colored(ongroundls[30],"grey",rock[11])+colored(ongroundls[31],"grey",rock[12]))
while pos > 19:
ongroundls[pos] = "#"
pos - 1
if pos == random.randrange(0, 32, 1):
pos == random
print(colored(ongroundls[0],"grey","on_cyan")+colored(ongroundls[1],"grey","on_cyan")+colored(ongroundls[2],"grey","on_cyan")+colored(ongroundls[3],"grey","on_cyan")+colored(ongroundls[4],"grey","on_cyan")+colored(ongroundls[5],"grey","on_cyan")+colored(ongroundls[6],"grey","on_cyan")+colored(ongroundls[7],"grey","on_cyan")+colored(ongroundls[8],"grey","on_cyan")+colored(ongroundls[9],"grey","on_cyan")+colored(ongroundls[10],"grey","on_cyan")+colored(ongroundls[11],"grey","on_cyan")+colored(ongroundls[12],"grey","on_cyan")+colored(ongroundls[13],"grey","on_cyan")+colored(ongroundls[14],"grey","on_cyan")+colored(ongroundls[15],"grey","on_cyan")+colored(ongroundls[16],"grey","on_cyan")+colored(ongroundls[17],"grey","on_cyan")+colored(ongroundls[18],"grey","on_cyan")+colored(ongroundls[19],"grey",rock[0])+colored(ongroundls[20],"grey",rock[1])+colored(ongroundls[21],"grey",rock[2])+colored(ongroundls[22],"grey",rock[3])+colored(ongroundls[23],"grey",rock[4])+colored(ongroundls[24],"grey",rock[5])+colored(ongroundls[25],"grey",rock[6])+colored(ongroundls[26],"grey",rock[7])+colored(ongroundls[27],"grey",rock[8])+colored(ongroundls[28],"grey",rock[9])+colored(ongroundls[29],"grey",rock[10])+colored(ongroundls[30],"grey",rock[11])+colored(ongroundls[31],"grey",rock[12]))
in while loop (buffer is just a small delay and a os.system('cls')
obstacle()
buffer()
rannum = random.randrange(1,20,1)
it was working fine, then I made some minor changes and i cannot seem to fix it. i have tried changing the randrange, and some things are commented out in an attempt to fix it. , so the numbers are not what they were when the problem started. What could I do to fix it?
Your problem is the final line. You hard-coded rock[12] at the end of the print, but rock only has 12 elements, so the last valid index is 11.
As I noted in the comments, even if you fix this, the code is borked; your while loop will either never run, or never exit, because you never change pos within the loop (pos - 1 computes a new value, but never stores it; pos -= 1 is perhaps the intent).
I want to create several multiplication tables by telling this python program how many I want to make. Then, have the program create that number of multiplication tables saved to that same number of created .txt files. I want one table per .txt file. What code do I need to add and how do I do it? Thanks for your time.
This is the program I want to add the functionality to:
def tablep():
n=int(input("Enter a Number:"))
start=int(input("Enter a start:"))
end=int(input("Enter an end:"))
file=open("*table.txt","a")
if start<end:
for i in range(start,end+1):
s = str(n) + "*" + str(i) + "= " + str(n*i)
file.write(s)
file.write("\n")
print(n,"*",i,"=",n*i)
elif start>end:
for i in range(start,end,-1):
s = str(n) + "*" + str(i) + "=" + str(n * i)
file.write(s)
file.write("\n")
print(n, "*", i, "=", n * i)
file.close()
w = tablep()
Example output I want in each file to have. This is just one of the files containing the multiplication table with 3:
3*0= 0
3*1= 3
3*2= 6
3*3= 9
From my understanding of your question, you want each txt file to be unique based on the start and end inputs, so you need to just loop over n times:
def tablep():
n=int(input("Enter a Number:")) # number of txt files
for x in range(0, n):
start=int(input("Enter a start:"))
end=int(input("Enter an end:"))
f_path = "table_" + str(x) + ".txt" # this will numerate each table
file = open(f_path, 'w+') # 'w+' tag will create a file if it doesn't exist
if start<end:
for i in range(start,end+1):
s = str(n) + "*" + str(i) + "= " + str(n*i)
file.write(s)
file.write("\n")
print(n,"*",i,"=",n*i)
elif start>end:
for i in range(start,end,-1):
s = str(n) + "*" + str(i) + "=" + str(n * i)
file.write(s)
file.write("\n")
print(n, "*", i, "=", n * i)
file.close()
I have two lists:
D1=[["a "," "," "," "," "," "],["b "," ","o"," "," "," "],["c ","x"," "," "," "," "],["d "," "," "," "," "," "],["e "," "," "," "," "," "]]
D2=[["a "," ","o"," ","x"," "],
["b "," "," "," "," "," "],["c "," "," "," "," "," "],["d "," "," "," "," "," "],["e "," "," "," "," "," "]]
D=[]
I want to make a list D so,D[i]=D1[i] + D2[i],for example the first element(list) looks like this:
D=[["a "," "," "," "," "," ","a "," ","o"," ","x"," "],...]
Please help me I am new in python
Try this:
D = [i+j for i,j in zip(D1,D2)]
if lengths are different it will cut the rest and go forward to the minimum of D1, D2. if you want the opposite, use zip_longest, like this:
from itertools import zip_longest
D = [i+j for i,j in zip_longest(D1,D2)]
But both will work if D1 and D2 have the same length.
Straightforward from your will (D[i] = D1[i] + D2[i]), the simplest way is to use comprehension lists. Asumming len(D1) == len(D2), :
D = [ D1[i] + D2[i] for i in range(len(D1)) ]
will do the job.
First copy D1 to D, if you don't want to alter D1. Then use extend method in python. It'll add all elements of list2 to list1.
Here is a simple code: though the time complexity of this code is O (n^2), it can be improved.
D1=[["a "," "," "," "," "," "],["b "," ","o"," "," "," "],["c ","x"," "," "," "," "],["d "," "," "," "," "," "],["e "," "," "," "," "," "]]
D2=[["a "," ","o"," ","x"," "],
["b "," "," "," "," "," "],["c "," "," "," "," "," "],["d "," "," "," "," "," "],["e "," "," "," "," "," "]]
D = D1 [:]
for i in range (len (D)):
D[i].extend (D2 [i])
print D
I am trying to write a code for myself that will give me an answer for simple interest, I will use same concept and later make compound interest. I am having trouble with my rate. when I do it as a percentage like
r = int(input("rate %: ")
and I type 5.4 it does not work so I tried it in a decimal form like this
r = int(input("Rate 0."))
i get the same answer at end if i do 0.045 and 0.45
so how do i fix this problem
here is my entire code
while True:
while True:
print('Working out for SIMPLE INTEREST')
p = int(input("Principl:"))
r = int(input("Rate 0."))
t = int(input("Time yrs:"))
i = p*r
i = i*t
a = p + i
print("Interest = " + str(i))
print("Accumalated = " + str(a))
print(str(p) + ' x ' + str(r) + ' x ' + str(t) + ' = ' + str(i) + ' | ' + str(p) + ' + ' + str(i) + ' = ' + str(a))
int converts the input string to an integer, which is a whole number like 4 or 5. For 5.4, you want a floating point number, which you can make using the float function:
r = float(input("rate %: "))
(For professional usage, you might even consider the arbitrary-precision decimal package, but it's probably overkill in your situation.)
It is because int does not support decimal numbers
So change int(input('something...')) to input('sonething...')
I am learning python so this might sound simple, I am trying to run the code below but I keep getting the error message shown, any thoughts on what could be causing it?
from geopy import geocoders
import csv
g_api_key = 'my_google_api_key'
g = geocoders.GoogleV3(g_api_key)
costco = csv.reader (open('costcolimited.csv'), delimiter = ',')
# Print header
print "Address, City, State, Zip Code, Latitude, Longitude"
for row in costco:
full_addy = row[1]+ "," + row[2]+ "," + row[3] + "," + row[4]
place, (lat,lng) = list (g.geocode(full_addy, exactly_one=FALSE))[0]
full_addy + "," + str(lat) + "," + str(lng)
The error I am getting
Traceback (most recent call last):
File "C:\Python27\geocodelocations.py", line 12, in <module>
full_addy = row[1]+ "," + row[2]+ "," + row[3] + "," + row[4]
IndexError: list index out of range
The error is caused by referring element out of list range. From your code, it is probably because you start counting element from 1. Remember that in python list, index starts from 0. If this is your situation, then shift indexes in
full_addy = row[1]+ "," + row[2]+ "," + row[3] + "," + row[4]
to
full_addy = row[0]+ "," + row[1]+ "," + row[2] + "," + row[3]
Otherwise, check your data structure and make sure it is matched with your code.
Thanks
First of all make sure your CSV file has four columns. Try checking if len(row) >= 4. If it contains you can go on with your code, but the first item in a Python list is referenced by 0 index.
Try something like this:
for row in costco:
if len(row) < 4:
print "Invalid file!"
break
full_addy = row[0]+ "," + row[1]+ "," + row[2] + "," + row[3]
place, (lat,lng) = list (g.geocode(full_addy, exactly_one=FALSE))[0]
full_addy + "," + str(lat) + "," + str(lng)