使用raw_input进行操作

Kan*_*ura 0 python dictionary list python-2.7

我有5个整数列表.

它们是在数小时内观察到的流星数量.

# Nights = [11pm,12pm,1am,2am,3am]
mon = [2,4,1,3,2]
tue = [3,2,4,3,3]
wed = [1,2,1,1,1]
thu = [4,3,2,3,4]
fri = [2,1,2,1,1]
Run Code Online (Sandbox Code Playgroud)

我想使用raw_input向我询问一个晚上和一个时间,然后它会将值返回给我.

night = raw_input('Which night? mon,tue,wed,thu or fri. ')
time = raw_input('Which time? t11,t12,t01,t02 or t03. ')
t11,t12,t01,t02,t03 = 0,1,2,3,4
print night[time]
Run Code Online (Sandbox Code Playgroud)

这里的问题是'night'raw_input是一个字符串,不能用于夜晚[时间]

TypeError: cannot concatenate 'str' and 'float' objects
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

the*_*eye 7

您可以将与日期对应的流星事件存储为字典,如下所示

occurrences = dict(
    mon = [2, 4, 1, 3, 2],
    tue = [3, 2, 4, 3, 3],
    wed = [1, 2, 1, 1, 1],
    thu = [4, 3, 2, 3, 4],
    fri = [2, 1, 2, 1, 1]
)
Run Code Online (Sandbox Code Playgroud)

您也可以将时间存储为字典

times = dict(
    t11 = 0
    t12 = 1,
    t01 = 2,
    t02 = 3,
    t03 = 4
)
Run Code Online (Sandbox Code Playgroud)

现在,只需从times和出现的索引中获取索引occurrences并显示它们,就像这样

night = raw_input('Which night? mon,tue,wed,thu or fri. ')
time = raw_input('Which time? t11,t12,t01,t02 or t03. ')
print occurrences[night][times[time]]
Run Code Online (Sandbox Code Playgroud)