Python can';t不使用range()或len()遍历元素
我尝试使用for循环来迭代输入(字符串)的长度,但是当我尝试使用len()或range()时,他们给了我一个错误,告诉我类型是错误的 这段代码是奶牛和公牛游戏的开始。 已尝试将对象类型更改为列表。 在升华文本和空闲文本之间切换。 我使用type()方法检查了输入的类型 程序输出:Python can';t不使用range()或len()遍历元素,python,python-3.x,Python,Python 3.x,我尝试使用for循环来迭代输入(字符串)的长度,但是当我尝试使用len()或range()时,他们给了我一个错误,告诉我类型是错误的 这段代码是奶牛和公牛游戏的开始。 已尝试将对象类型更改为列表。 在升华文本和空闲文本之间切换。 我使用type()方法检查了输入的类型 程序输出: Cows and Bulls! guess 4 numbers: 1111 Traceback (most recent call last): File "D:\Computers\Programming\Pyt
Cows and Bulls!
guess 4 numbers: 1111
Traceback (most recent call last):
File "D:\Computers\Programming\Python\Codes\Exercises\17.py", line 8, in <module>
for item in len(guess):
TypeError: 'int' object is not iterable
Cows and Bulls!
guess 4 numbers: 1111
Traceback (most recent call last):
File "D:\Computers\Programming\Python\Codes\Exercises\17.py", line 8, in <module>
for item in range(guess):
TypeError: 'str' object cannot be interpreted as an integer
牛和公牛!
猜4个数字:1111
回溯(最近一次呼叫最后一次):
文件“D:\Computers\Programming\Python\code\Exercises\17.py”,第8行,在
对于len中的项目(猜测):
TypeError:“int”对象不可编辑
牛和公牛!
猜4个数字:1111
回溯(最近一次呼叫最后一次):
文件“D:\Computers\Programming\Python\code\Exercises\17.py”,第8行,在
对于范围内的项目(猜测):
TypeError:“str”对象不能解释为整数
您需要组合范围()
和len()
。利用
for index in range(len(guess)):
# your code here
您可以迭代猜测的长度
您也可以直接迭代guess
,但由于还需要每个字符的索引,因此需要使用enumerate()
。这将为每个字符返回两个值,第一个是索引,第二个是字符本身。所以你会这样做:
for index, c in enumerate(guess):
# your code here with index as the index and c as the character
您的问题是,
input
获取的guess
是字符串类型(与输入一样)。然后,len(guess)
的类型显然是int。不能迭代int,因为它不是序列。您有几个选项可以解决此问题:
范围内的i(len(guess)):
如果int(猜测[i])==目标[i]:
用于枚举中的i,c(猜测):
如果int(c)=目标[i]:
guess=input('Cows and Bulls!\n按4个数字:'))
猜测=[int(c)表示猜测中的c]
对于范围内的i(len(猜测)):
如果猜测[i]==目标[i]:
也可以使用:guesses=list(map(int,guess))
input()
python3中的函数将eac值作为str
因此,您需要使用int()
函数将其转换为int
使用以下代码进行检查:
import random
target = [random.randint(0, 9) for i in range(4)]
turns = 0
all_cows = False
while not all_cows:
guess = input('Cows and Bulls!\nguess 4 numbers: ')
turns += 1
#Iterate String Directly
for item in guess:
#Convert Substring into int for using as Indices
if guess[int(item)] == target[int(item)]:
print('cow')
elif guess[int(item)] in target:
print('bull')
谢谢大家在那个案子上帮了我的忙。 似乎问题出在我不理解len()正确,我忘记了它返回的长度是int,当错误表示对象类型int时,我感到困惑 解决方案1:使用range(len(guess))遍历字符串的长度 解决方案2:直接遍历字符串-这不是我搜索的内容,因为我想使用“item”对字符串进行索引 解决方案3:使用我从未听说过的enumerate(guess)(因此,如果有人感到无聊并想解释它,我会感激地接受解释!
编辑:得到我的答案。谢谢你的回答。你能详细说明枚举方法吗?我尝试将字符串转换为列表(显然发生了相同的错误(这是使用range(len())求解的)那么,在这种情况下,将字符串转换为列表有什么意义呢?@NanashiSenpai我已经用链接更新了我的答案。简言之,enumerate返回一个元组,由列表中的(索引、项)组成。因此它基本上为您节省了
guess[I]
。至于列表,这只是一个前瞻性的问题。您已经从一开始就准备好了数据,这为您节省了int(…)
那么i=index和c=item?那么迭代是如何工作的?它以什么方式迭代索引和项?就像对l中的x进行一样:
你依次在l
中得到每个元素,这里你得到c
中的元素及其在i
中的索引。所以你不需要循环索引,然后进行猜测[i]
,因为您已经免费拥有了……请查看链接
import random
target = [random.randint(0, 9) for i in range(4)]
turns = 0
all_cows = False
while not all_cows:
guess = input('Cows and Bulls!\nguess 4 numbers: ')
turns += 1
#Iterate String Directly
for item in guess:
#Convert Substring into int for using as Indices
if guess[int(item)] == target[int(item)]:
print('cow')
elif guess[int(item)] in target:
print('bull')