'I'm trying to make a mastermind program
I've been trying to make a mastermind program but it keeps giving a "int" object is not subscriptable error. Any help would be appreciated
This is the code I made
import random
num = random.randint(999,9999)
gnum = []
x = input("enter a 4 digit number")
gnum.append(x[0])
gnum.append(x[1])
gnum.append(x[2])
gnum.append(x[3])
correct = 0
for i in range(0,4):
if num[i] == gnum[i]:
correct += 1
print(num)
print(gnum)
Solution 1:[1]
you can convert integer to string. So you can simply change the num variable to num = str(num).
import random
num = random.randint(999,9999)
num = str(num)
gnum = []
x = input("enter a 4 digit number ")
gnum.append(x[0])
gnum.append(x[1])
gnum.append(x[2])
gnum.append(x[3])
correct = 0
for i in range(0,4):
if num[i] == gnum[i]:
correct += 1
print(num)
print(gnum)
print(correct)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chathura Madhusanka |
