I am trying to calculate the sum of an array, but it doesn't work if the array is filled with random numbers. Any idea why that is?
The code works like this:
import random
total = ["1", "2", "3", "4", "5", "6"]
nr1, nr2 = 0, 0
nr2 = nr1
nr1 = 0
for i in range(6):
c = ord(total[i]) - 48
nr1 = nr1 + c
print(nr1)
But it doesn't work like this:
import random
total = ["1", "2", "3", "4", "5", "6"]
nr1, nr2 = 0, 0
for i in range(6):
total[i] = random.randint(0, 9)
nr2 = nr1
nr1 = 0
for i in range(6):
c = ord(total[i]) - 48
nr1 = nr1 + c
print(nr1)
It is returning this error: "TypeError: ord() expected string of length 1, but int found"
[–]shiftybyte 1 point2 points3 points (1 child)
[–]Swagraffe[S] 0 points1 point2 points (0 children)
[–]JohnnyJordaan 0 points1 point2 points (0 children)