我已将变量转换为字符串,但是 Python 仍然无法识别这一点,并表示该整数不可下标。
我已经尝试使用相同的“整数不可下标”问题查看其他问题,但没有一个专门回答我的问题。
在发生错误之前,我已将变量显式转换为字符串。
import random
num = random.randint(1000, 9999)
tot_correct = 0
tot_tries = 0
while tot_correct != 4:
tot_correct = 0
tot_tries += 1
guess = input("Guess the number: ")
guess = str(guess)
#check 1st number
if guess[0] == num[0]:
tot_correct += 1
#check 2nd number
if guess[1] == num[1]:
tot_correct += 1
#check 3rd number
if guess[2] == num[2]:
tot_correct += 1
#check 4th number
if guess[3] == num[3]:
tot_correct += 1
print("You got " + tot_correct + " numbers right.")
print("You have guessed the number correctly! It took you " + tot_tries + " tries.")
我希望该字符串成为一个字符串数组,(但它仍然没有,并返回相同的错误),然后确定单个数字是否与已经匹配的数字匹配
qq_遁去的一_1
蝴蝶不菲
相关分类