简体   繁体   English

为什么此代码会产生错误“ int无法下标”?

[英]Why does this code produce the error 'int is not subscriptable'?

I have converted the variable to a string, however Python still does not recognise this and says the integer is not subscriptable. 我已将变量转换为字符串,但是Python仍然无法识别该字符串,并说该整数不可下标。

I've already tried viewing other questions with the same 'integer is not subscriptable' problem but none which answer my question specifically. 我已经尝试查看具有相同“整数不可下标”问题的其他问题,但是没有一个问题可以专门回答我的问题。

I have explicity converted the variable to a string the line before the error occurs. 我已经明确将变量转换为字符串,然后再发生错误。

 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.")

I expected the string to become a string array, (but it still does not, and returns the same error) and then identify whether or not the individual number matches the one already 我希望该字符串成为一个字符串数组(但仍然不会,并返回相同的错误),然后确定单个数字是否已与该数字匹配

Your code isn't doing what you think it is. 您的代码没有按照您的想法去做。 Right now you are inputting a number, converting it to a string and comparing the first character of that guess string to the first index of the number num[0] which isnt indexable. 现在,您正在输入一个数字,将其转换为字符串,然后将该猜测字符串的第一个字符与无法索引的数字num[0]的第一个索引进行比较。

edit: Your code is doing a number of things wrong actually. 编辑:您的代码实际上在做很多错误。 One huge problem you have is you are setting tot_correct = 0 inside of your while loop which means it'll run forever and never finish. 您遇到的一个巨大问题是,您正在while循环中设置tot_correct = 0 ,这意味着它将永远运行并且永远不会完成。

But stepping back I think you are making this problem too complicated. 但是退后一步,我认为您使这个问题变得太复杂了。 Let's talk about the pseudocode for what I believe you are trying to do. 让我们谈谈我相信您正在尝试执行的伪代码。

num_guessed = 0
number_to_guess = 4
total_guesses = 0
while num_guessed < number_to_guess:
    # each pass we reset their guess to 0 and get a new random number
    guess = 0
    # get a new random number here
    while guess != random:
        # have a user guess the number here
        total_guesses += 1 # we can increment their total guesses here too
        # it would be a good idea to tell them if their guess is higher or lower
        # when they guess it right it will end the loop

    num_guessed += 1

# down here we can tell them game over or whatever you want

The code should at least give you an idea of how to approach the problem without solving it for you. 该代码至少应使您了解如何解决问题而不为您解决。

I respectfully disagree with the previous comment. 我谨不同意先前的评论。 It will be possible for the loop to end. 循环可能会结束。 I understand why you are setting tot_correct to 0 at the start of each loop. 我了解您为什么在每个循环开始时将tot_correct设置为0。 Because tot_correct is incremented up to 4 times, it is possible for tot_correct == 4 to be true. 因为tot_correct最多增加4次,所以tot_correct == 4可能为true。

Edit: The poster is trying to count the correct number of digits provided. 编辑:海报试图计算提供的正确位数。 So if the number to guess is '1234' and the user inputs '1564', the poster wants the code to return '2' to indicate that the '1' and '4' were correct numbers. 因此,如果要猜测的数字是“ 1234”,并且用户输入“ 1564”,则发布者希望代码返回“ 2”,以指示“ 1”和“ 4”是正确的数字。 It's like the game mastermind, where a player has to guess the correct colors and orientation of the colors. 就像游戏策划者一样,玩家必须猜测正确的颜色和颜色的方向。 However, this code will not inform the user if a correct number is added in an incorrect position, just if the correct number is in the correct position. 但是,即使正确的数字位于正确的位置,此代码也不会通知用户是否添加了正确的数字。

However, he is correct that your error lies in your access of num[<index>] . 但是,他是正确的,您的错误在于您对num[<index>] num is an integer so you cannot index into it, hence 'integer is not subscriptable.' num是一个整数,因此您无法对其进行索引,因此“整数不可下标”。 num needs to be a string in order to index the characters. num必须是字符串才能对字符进行索引。

Edit: guess was already a string without the call to str() because the return from input() is a string 编辑: guess已经是一个没有调用str()的字符串,因为从input()返回的是一个字符串

Some things to consider: Do you want your user to know they need a 4 digit number? 需要考虑的一些事情:您是否要让用户知道他们需要4位数字? What if there are whitespaces added? 如果添加了空格怎么办? Currently your code does not remove whitespace. 当前,您的代码无法删除空格。 If you are looking for '6543' as the magic number and I enter ' 6543' your solution would not recognize my answer as correct. 如果您正在寻找“ 6543”作为幻数,而我输入的是“ 6543”,则您的解决方案将无法识别我的答案是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM