cs50 pset6 Credit IndexError: string index out of range - python

When trying to launch the program, I get the "IndexError: string index out of range" error on line 19. Is it because I forgot to terminate the string or turn it back to int? Very new to python, so any help or trivia would be appreciated.
I tried doing something like thisdoubleCheck = int(strNumber[i] * 2) , but it did not solve the problem. Am I doing it wrong?
Here is the full code just in case of later errors.
from cs50 import get_int
import sys
ccNumber = get_int("Number: ")
strNumber = str(ccNumber)
Numlen = len(str(ccNumber))
if Numlen != 13 and Numlen != 15 and Numlen != 16:
sys.exit()
firstSum = 0
secondSum = 0
doubleCheck = 0
for i in range(Numlen, -1, -1):
if i % 2 == 0:
secondSum = secondSum + strNumber[i]
if i % 2 == 1:
doubleCheck = strNumber[i] * 2
if doubleCheck >= 10:
firstSum = firstSum + (doubleCheck % 10)
firstSum = firstSum + (doubleCheck / 10)
else:
firstSum += doubleCheck;
totalChecksum = firstSum + secondSum
if totalChecksum % 10 == 0:
if strNumber[0] == 3 and Numlen == 15:
if strNumber[1] == 4 or strNumber[1] == 7:
print("AMEX", end="");
elif strNumber[0] == 4:
if Numlen == 13 or Numlen == 16:
print("VISA", end="")
elif strNumber[0] == 5 and Numlen == 16:
if strNumber[1] == 1 or strNumber[1] == 2 or strNumber[1] == 4 or strNumber[1] == 5:
print("MASTERCARD", end="")
else:
print("INVALID", end="")

Python string indexing starts at 0. For an N-length string, valid indexes are 0 through N-1. Index N is out of range.
In the for loop, you're accessing strNumber[i]. On the first iteration, i is equal to Numlen, which is out of range.
Perhaps you intended to start the loop at Numlen - 1?

Related

I have multiple variables which sometimes have the same value, but not always, how to specify which part of the function to use python

I have this code:
AWomen = BWomen = CWomen = DWomen = EWomen = 1
def pointscalcwomen(value):
if value == AWomen:
print("AWomen")
if value < 12:
womenspoints = 105 - 5 * (int(AWomen))
elif value < 61:
womenspoints = 61 - AWomen
else:
womenspoints = 1
elif value == BWomen:
print("BWomen")
if value < 7:
womenspoints = 80 - 5 * (int(BWomen))
elif value < 56:
womenspoints = 56 - BWomen
else:
womenspoints = 1
elif value == CWomen:
print("CWomen")
if value < 51:
womenspoints = 51 - CWomen
else:
womenspoints = 1
elif value == DWomen:
print("DWomen")
if value < 31:
womenspoints = 31 - DWomen
else:
womenspoints = 1
elif value == EWomen:
print("EWomen")
if value < 12:
womenspoints = 105 - 5 * (int(EWomen))
elif value < 61:
womenspoints = 61 - EWomen
else:
womenspoints = 1
return womenspoints
for row in reader:
if "A" in row[0]:
points = pointscalcwomen(AWomen)
print(points)
AWomen = AWomen + 1
elif "B" in row[0]:
points = pointscalcwomen(BWomen)
print(points)
BWomen = BWomen + 1
elif "C" in row[0]:
points = pointscalcwomen(CWomen)
print(points)
CWomen = CWomen + 1
elif "D" in row[0]:
points = pointscalcwomen(DWomen)
print(points)
DWomen = DWomen + 1
elif "E" in row[0]:
points = pointscalcwomen(EWomen)
print(points)
EWomen = EWomen + 1
AWomen/BWomen etc. is category as Category A women, I have AMen, BMen etc. but with the men I am unlikely to ever encounter this problem.
However, I am facing the problem that if A is never in row[0], then the first B will get 100 points, not 75 etc.
This is because at that point value == AWomen == BWomen, is there a way to get around this. I have thought of changing my code to something like:
elif "B" in row[0]:
A = 10000
points = pointscalcwomen(BWomen)
B = B + 1
This would make the script work most of the time as in the CSV row[0] will generally follow the pattern:
A
A
A
A
B
B
B
C
C
However, if there is the case where it is:
A
A
B
A
B
B
then it would not work as the A in the fourth row who was beaten by a B would get 1 point instead of 90.

TypeError: not all arguments converted during string formatting, Python

I dont know why the error is coming up. Its a program to show the numbers that include a 7 and are a multiple of 7.
import sys
Numbers = raw_input()
Answer = 0
NumbersList = Numbers.split()
Length = len(NumbersList)
for num in range(0, Length):
N = NumbersList [num-1]
ListNumber = list(str(N))
if (N == 0):
print(Answer)
elif (num == Length):
print(Answer)
elif (N % 7 == 0):
Answer = Answer + 1
elif 7 in ListNumber:
Answer = Answer + 1
sys.stdout.flush()
New Question. My elif statement at the bottom doesnt seem to work.
The input is a list of numbers from 0-100. Stop at a 0. Answer = Numbers with %7 = 0 or with a 7 in it
import sys
Numbers = raw_input()
Answer = 0
NumbersList = Numbers.split()
Length = len(NumbersList)
for num in range(0, Length):
N = NumbersList [num]
ListNumber = list(str(N))
if (int(N) == 0):
print(Answer)
break
elif (num == Length):
if (int(N) % 7 == 0):
Answer = Answer + 1
elif 7 in ListNumber:
Answer = Answer + 1
print(Answer)
elif (int(N) % 7 == 0):
Answer = Answer + 1
elif 7 in ListNumber:
Answer = Answer + 1
sys.stdout.flush()
Stealing #PeterWood's comment, you need to convert the input to an integer first.
N = int( NumbersList [num-1] )
You may also want to consider using for num in NumberList and reading https://www.python.org/dev/peps/pep-0008/ for a guide to good practice.
This works for me (or doesn't error):
import sys
Numbers = raw_input()
Answer = 0
NumbersList = Numbers.split()
Length = len(NumbersList)
for num in range(0, Length):
N = int( NumbersList [num-1])
ListNumber = list(str(N))
if (N == 0):
print(Answer)
elif (num == Length):
print(Answer)
elif (N % 7 == 0):
Answer = Answer + 1
elif 7 in ListNumber:
Answer = Answer + 1
print Answer
sys.stdout.flush()

python: I have this code (below) but it prints in reverse, how can I reverse it

How can I reverse the output?
def dectohex(num):
z = int(0)
y = int(0)
if num > 0:
z = num // 16
y = int((num % 16))
if y == 10:
print("A", end = "")
elif y == 11:
print("B", end = "")
elif y == 12:
print("C", end = "")
elif y == 13:
print("D", end = "")
elif y == 14:
print("E", end = "")
elif y == 15:
print("F", end = "")
elif y == 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9:
print(y, end = "")
dectohex(z)
inp = int(input("Enter number "))
dectohex(inp)
Calling the recursive function earlier will reverse the output:
def dectohex(num):
if num > 0:
z = num // 16
dectohex(z)
y = num % 16
if y == 10:
print("A", end = "")
elif y == 11:
print("B", end = "")
elif y == 12:
print("C", end = "")
elif y == 13:
print("D", end = "")
elif y == 14:
print("E", end = "")
elif y == 15:
print("F", end = "")
else:
print(y, end = "")
Note that I also made some other optimization to the function. Notably, I removed unnecessary initialization, casting and simplified the if-chain, since the number y is known to be an integer with 0 <= y <= 15.

Python Error: unsupported operand type(s) for +: 'int' and 'NoneType'

I don't understand this error OR what it means. I will paste my code underneath, but I don't think it is really relevant; I just want to understand this error.
It's just a bit of code to add up the letters in all numbers 1 - 1000 (inclusive)
def number_translator(x):
if x == 1:
return 3
elif x == 2:
return 3
elif x == 3:
return 5
elif x == 4:
return 4
elif x == 5:
return 4
elif x == 6:
return 3
elif x == 7:
return 5
elif x == 8:
return 5
elif x == 9:
return 4
elif x == 10:
return 3
elif x == 11:
return 6
elif x == 12:
return 6
elif x == 14:
return 8
elif x == 15:
return 7
elif x == 16:
return 7
elif x == 17:
return 9
elif x == 18:
return 8
elif x == 19:
return 8
elif x == 20:
return 6
elif x == 30:
return 6
elif x == 40:
return 5
elif x == 50:
return 5
elif x == 60:
return 5
elif x == 70:
return 7
elif x == 80:
return 6
elif x == 90:
return 6
count = 0
for element in range(1, 1001):
if element < 21:
count += number_translator(element) # for numbers 1 - 20
elif 20 < element < 100:
count += number_translator(int(str(element)[0]) * 10) + number_translator(int(str(element)[1])) # for numbers 21 through 100
elif element % 100 == 0 and element != 1000:
count += number_translator(int(str(element)[0])) + 7 # for numbers divisible by 100, but not 1000
elif element == 1000:
count += 11 # just for 1000
elif element % 100 < 20:
count += number_translator(int(str(element)[0])) + 10 + number_translator(int(str(element)[1:3])) # now I add in numbers like 101 - 120, 201 - 220, etc.
else:
count += number_translator(int(str(element)[0])) + 10 + number_translator(int(str(element)[1]) * 10) + number_translator(int(str(element)[2])) # now the rest( 121, 122, 123, 225, 256, 984, etc.)
print(count)
When none of the if test in number_translator() evaluate to true, the function returns None. The error message is the consequence of that.
Whenever you see an error that include 'NoneType' that means that you have an operand or an object that is None when you were expecting something else.
In your giant elif chain, you skipped 13. You might want to throw an error if you hit the end of the chain without returning anything, to catch numbers you missed and incorrect calls of the function:
...
elif x == 90:
return 6
else:
raise ValueError(x)
I got a similar error with '/' operand while processing images. I discovered the folder included a text file created by the 'XnView' image viewer. So, this kind of error occurs when some object is not the kind of object expected.

Python says: "IndexError: string index out of range."

I am making some practice code for a game similar to the board game, MasterMind-- and It keeps coming out with this error, and I can't figure out why it's doin it. Here's the code:
def Guess_Almost (Guess, Answer):
a = ''.join([str(v) for v in Answer])
g = str(Guess)
n = 0
am = 0
while n < 5:
if g[n] == a[0]:
am = am + 1
if g[n] == a[2]:
am = am + 1
if g[n] == a[3]:
am = am + 1
if g[n] == a[3]:
am = am + 1
n = n + 1
return(am)
Okay, the Guess is specified to be 4 integers, and the Answer is a list containing 4 numbers. They both have the same 'len' after the code, so i don't have a clue.
The point of this code is to turn the Answer into a string of 4 numbers, and see if any of those numbers match thoise of the guess, and return how many total matches there are.
See if this helps
def Guess_Almost (Guess, Answer):
a = ''.join([str(v) for v in Answer])
g = str(Guess)
n = 0
am = 0
if len(g) >= 5 and len(a) >=4:
while n < 5:
if g[n] == a[0]:
am = am + 1
if g[n] == a[2]:
am = am + 1
if g[n] == a[3]:
am = am + 1
if g[n] == a[3]:
am = am + 1
n = n + 1
return(am)

Categories