繁体   English   中英

用户输入后的python操作列表

[英]python manipulating list after user input

months=['January','February','March','April','May','June','July','August','September','October','November','December']
usermonth=input('Enter the month: ')
if usermonth >=months[0]and usermonth <=months[10]:
   nextmonth=months(usermonth+1)
   print(nextmonth)

我将如何在用户月份的索引中定义一个? 谢谢。

我想您正在尝试显示用户提供的月份之后的月份:

 months.index(usermonth)

将为您提供用户提供的月份的索引(在月份列表中)。

然后,您可以测试您月份的指数是否不超过11​​:

months=['January','February','March','April','May','June','July','August','September','October','November','December']
usermonth=input('Enter the month: ')
if months.index(usermonth) < 11:
   print(months[months.index(usermonth) + 1])

我相信您要在下个月打印,由用户插入:

为此,您需要更好地理解arrays索引 (或者换句话说 :如何访问数据列表)。

您在if语句中正确使用了索引,但是,您没有在试图定义nextMonth的行中nextMonth 进行months()调用关键字months就好像它是一个函数一样:并且您需要将其作为列表调用,因此,请使用带有[]索引 :)

暂无
暂无

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

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