Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中用户输入的索引处打印值_Python_List_Python 3.x_Indexing_Printing - Fatal编程技术网

在Python中用户输入的索引处打印值

在Python中用户输入的索引处打印值,python,list,python-3.x,indexing,printing,Python,List,Python 3.x,Indexing,Printing,我对Python编程非常陌生,这似乎很简单,但我似乎无法正确地进行编程 我有一个值列表。 我想提示用户输入。 然后打印出列表中相应索引号处的值 myList [0, 1, 20, 30, 40] choice = input() print (......) 如果用户输入2,我想打印索引2(20)处的值。我不确定打印后要放什么。您可以使用用户给定的输入访问列表来完成此操作 我假设用户提供的输入是字符串,因此我们需要使用int()将其更改为整数 print myList[int(

我对Python编程非常陌生,这似乎很简单,但我似乎无法正确地进行编程

我有一个值列表。
我想提示用户输入。
然后打印出列表中相应索引号处的值

myList [0, 1, 20, 30, 40]    
choice = input()    
print (......)

如果用户输入2,我想打印索引2(20)处的值。我不确定打印后要放什么。

您可以使用用户给定的输入访问列表来完成此操作

我假设用户提供的输入是字符串,因此我们需要使用
int()
将其更改为整数

print myList[int(choice)]
此外,您可能需要首先检查所提供输入的有效性;不能小于0,也不能大于列表的长度-1

choice = int(choice)
if (choice < 0 || choice >= len(myList))
   print('Not Valid')
else
   print myList[int(choice)]
choice=int(choice)
如果(选项<0 | |选项>=len(myList))
打印('无效')
其他的
打印myList[int(选项)]

你说的是什么编码语言?Python,对不起,我应该提到这一点!哈坦克斯,伙计!与()else打印(myList[int(choice)]配合良好