简体   繁体   English

我对字符串和整数有些困惑,并且不断收到此错误:TypeError:列表索引必须是整数或切片,而不是str

[英]I'm a little confused with strings and integers, and I keep getting this error: TypeError: list indices must be integers or slices, not str

print("You may invite up to six people to your party.")
name = input("Enter invitee's name (or just press enter to finish): ")
nameList = ["","","","","",""]
currentName = 0

while name != "":
    if currentName > 5:
        break #If more than 6 names are input, while loop ends.
    else:
        nameList[currentName] = name
        name = input("Enter invitee's name (or just press enter to finish): ")
        currentName = currentName + 1

for i in len(nameList):
    invitee = nameList[i]

    print(invitee + ", please attend our party this Saturday!")

The only syntactic problem with your code is that you can't do for i in len(nameList) , you have to use range() if you want to loop a certain number of times. 您的代码唯一的语法问题是您无法for i in len(nameList)for i in len(nameList)如果要循环一定次数,则必须使用range() It will work if you change the last section to: 如果将最后一部分更改为:

for i in range(len(nameList)): # range(5) makes a list like [0, 1, 2, 3, 4]
    invitee = nameList[i]

    print(invitee + ", please attend our party this Saturday!")

len(nameList) returns an integer you should call range(len(nameList)) instead. len(nameList)返回一个整数,您应该改为调用range(len(nameList)) However, the code will be cleaner if you write it like: 但是,如果您这样编写代码,则代码将更加简洁:

print("You may invite up to six people to your party.")

name_list = []
for current_name in range(6):
    name = input("Enter invitee's name (or just press enter to finish): ")
    if not name:
        break
    name_list.append(name)

for invitee in name_list:
    print(invitee + ", please attend our party this Saturday!")

暂无
暂无

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

相关问题 我不断收到此错误:列表索引必须是整数或切片,而不是 str - I keep getting this error: list indices must be integers or slices, not str 我为什么会收到此错误TypeError:列表索引必须是整数或切片,而不是str - Why am I getting this error TypeError: list indices must be integers or slices, not str 我得到 TypeError:列表索引必须是整数或切片,而不是 str - i get TypeError: list indices must be integers or slices, not str 这是 python 在我尝试标记推文时返回的内容:TypeError: list indices must be integers or slices, not str - This is what python returns while I'm trying to tokenize tweets : TypeError: list indices must be integers or slices, not str 我错过了什么,因为我不断收到“类型错误:列表索引必须是整数或切片,而不是列表” - What am I missing because I keep getting a, "TypeError: list indices must be integers or slices, not list" 为什么我不断收到“列表索引必须是整数或切片,而不是 str”? - Why do I keep getting a “list indices must be integers or slices, not str”? “TypeError:list indices必须是整数或切片,而不是str” - “TypeError: list indices must be integers or slices, not str” TypeError:列表索引必须是整数或切片,而不是 str - TypeError: List indices must be integers or slices and not str 类型错误:列表索引必须是整数或切片,而不是 str - TypeError: list indices must be integers or slices, not str “TypeError:列表索引必须是整数或切片,而不是 str” - "TypeError: list indices must be integers or slices, not str"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM