Python,从输入中获取列表的值

时间:2016-08-22 10:43:49

标签: python

嗨,大家好我学习python,我有这个问题,因为没有创建1000"如果"功能我会这样做:

#Vari flag
print "choose flag"
print "(1) Syn"
print "(2) Ack"
print "(3) Push"
print "(4) Fin"
print "(5) Urg"
print "(6) Rst"

flag-list = ["--syn","--ack","--push","--fin","--urg","--rst"]
flag = raw_input(write number separated by comma: )

现在我会采用减号并将其转换为" flag-list"的文本。

1 个答案:

答案 0 :(得分:1)

# Note that variable names cannot contain hyphens
flag_list = ["--syn","--ack","--push","--fin","--urg","--rst"]

# This clearly has to be in quotes
user_input = raw_input("Enter numbers separated by comma:" )

# Split the user input string at the commas, and turn each element into an integer.
flag_nums = map(int, flag_num.split(','))

# List indexes start at 0, so subtract 1.
# Use brackets to access the Nth item in the list.
# This is a list comprehension.
flags = [flag_list[n - 1] for n in flag_nums]