[英]Converting a two digits integer in range into a list
I met a problem that when I was trying to add like '11' as one item in a list by using range(x,y), it doesn't work.(OK, 'IndexList' is just a silly mistake.) 我遇到一个问题,当我尝试使用range(x,y)将'11'作为一个项目添加到列表中时,它不起作用(好吧,'IndexList'只是一个愚蠢的错误。)
def choose_a():
indexList = []
indexOf = input("Enter the the index number: ")
for i in range(1,13):
indexList += str(i)
while indexOf not in IndexList:
indexOf = input("Enter the index number: ")
return indexOf
a = choose_a()
print(a)
When you i
is 11
, the following line: 当您
i
为11
,以下行:
indexList += str(i)
adds two '1'
s to the list, not '11'
. 在列表中添加两个
'1'
,而不是'11'
。
Problems I noticed: 我注意到的问题:
Use 采用
indexList.append(str(i))
instead of 代替
indexList += str(i)
In the while loop, use str(indexOf)
. 在while循环中,使用
str(indexOf)
。
Fix a typo in the while
line. 修正
while
行中的错字。 IndexList
-> indexList
IndexList
> indexList
Here's a working version. 这是工作版本。
def choose_a():
indexList = []
indexOf = input("Enter the the index number: ")
for i in range(1,13):
indexList.append(str(i))
while str(indexOf) not in indexList:
indexOf = input("Enter the index number: ")
return indexOf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.