Def函数未调用我创建的列表

时间:2019-03-09 08:42:29

标签: python python-3.x list function

我正在尝试制作一个简单的程序,以打印出由给定列表的第一个和最后一个元素组成的新列表。

default_list = [5, 10, 15, 20, 25]
first_list = [7, 233, 76, 234, 2]
second_list = [0, 5, 2, 6, 3423456]
third_list = [768, 56, 234, 765, 2434, 78676, 345, 467, 353, 765343]
new_list = []


def list_ends(chosen_list):
    new_list.append(chosen_list[0])
    new_list.append(chosen_list[len(chosen_list) - 1])
    print(new_list)


list_ends(input(": "))

但是发生的是,如果我在输入中输入一个单词说“ default_list”,它会给我一个答案[d,t],我相信这意味着它没有调用我创建的列表变量。

2 个答案:

答案 0 :(得分:0)

仅供参考,可以读取变量名称作为输入,如下所示:

list_name = input("List name: ")
if list_name in locals():
    list_ends(locals()[list_name])
else:
    print('List {} not found'.format(list_name))

但是,您的用例不需要这种解决方案。我敢肯定,有一种更好的方法可以做你想做的事情。例如,创建一个dict将字符串输入映射到列表,然后从输入中读取字符串,然后使用字典获取相应的列表。

答案 1 :(得分:0)

在Python 2中,awful = require("awful") local wibox = require("wibox") local naughty = require("naughty") local gears = require("gears") local beautiful = require("beautiful") xresources = require("beautiful.xresources") dpi = xresources.apply_dpi bar = {} -- make the sidebar bar.sidebar = wibox({ x = 0, y = 0, ontop = false, visible = false, width = beautiful.sidebar_width or dpi(450), bg = beautiful.sidebar_bg or "#2f2e3a", type = "dock", height = beautiful.sidebar_height or awful.screen.focused().geometry.height, }) -- Hide sidebar when mouse leaves too much from the sidebar -- It's incorporated along in the same table with the sidebar so the users -- can implement these however they want, e.g. in the 'keys.lua' module bar.sidebar_visible_limit = wibox({ x = 0, y = 0, ontop = false, visible = false, width = bar.sidebar.width + dpi(100), height = bar.sidebar.height, bg = '#000000', opacity = 0.3, --when it's all done this will be '0' }) bar.sidebar_visible_limit.input_passthrough = true -- Show sidebar when mouse touches edge local sidebar_displayer = wibox({ x = 0, y = 0, height = bar.sidebar.height, ontop = true, width = 1, visible = true, opacity = 0, input_passthrough = true }) function toggle_bar() -- they have to be in this order, so the sidebar will show first, -- and then the wibox that will close the sidebar when the mouse leaves -- second. If you do it the other way around, then if you go with the -- mouse on the sidebar-closing wibox , then if you try to go back -- to the sidebar, it will close it because it's 'left' the widget. -- That's why you have the sidebar-closing wibox on top and allow -- input_passthrough for the sidebar-closing wibox bar.sidebar.visible = not bar.sidebar.visible bar.sidebar.ontop = not bar.sidebar.ontop bar.sidebar_visible_limit.visible = not bar.sidebar_visible_limit.visible bar.sidebar_visible_limit.ontop = not bar.sidebar_visible_limit.ontop end bar.sidebar_visible_limit:connect_signal( "mouse::leave", toggle_bar ) sidebar_displayer:connect_signal( "mouse::enter", toggle_bar ) 函数实际上允许您键入Python表达式,但是在Python 3中,它仅返回一个字符串,即一个字符序列。

就像input"list"一样,字符串list与同名变量没有直接关系。

但是,您可以弥补这一差距:

"default_list"

在尝试进行此操作之前,您应该了解,由于多种原因,允许用户访问程序的内部是有问题的。例如,也许您应该提供一个带有编号选项的菜单?这样,用户将无法访问任意程序变量,只能访问您明确提供的变量。