gpt4 book ai didi

Printing information from a list of employees [closed](打印员工列表中的信息[已关闭])

转载 作者:bug小助手 更新时间:2023-10-28 11:18:26 25 4
gpt4 key购买 nike




I am new to this. I have created a list of information for 5 'employees'. I am trying to ask which employee the user wants to print the information for. How do I prompt the user to select from employee 1-5, and print the number of employee they select from the list I've created? I have this command-

我对此还是个新手。我已经为5名‘员工’创建了一份信息清单。我正在尝试询问用户想要打印哪位员工的信息。如何提示用户从员工1-5中选择,并打印他们从我创建的列表中选择的员工数量?我有这个命令-


pick = input('Select Employee 1-5 to print their information:') 

Then I have this list-

然后我就有了这个清单-


names = (employee1, employee2, employee3, employee4, employee5)

If they select employee1, I need it to print names[0]. If they select employee2, I need it to print names[1], and so on...

如果他们选择Emploe1,我需要它来打印姓名[0]。如果他们选择Emploe2,我需要它来打印姓名[1],以此类推……


I tried everything. I put:

我什么都试过了。我放了:


pick = input('Select Employee 1-5 to print their information:')
print(pick)
print(names(pick))

When it threw an error, I would put print(names[]) and it would say:

当它抛出一个错误时,我会输入print(names[]),它会说:


Select Employee 1-5 to print their information:ERROR!
Traceback (most recent call last):
File "<string>", line 69, in <module>
TypeError: tuple indices must be integers or slices, not str

Or I tried:

或者我试过了:


pick = input('Select Employee 1-5 to print their information:')
print(pick)
print(names[])

and it said:

上面写着:


"ERROR!
File "<string>", line 69
print(names[])
^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

更多回答

What’s the relevance of the activity-finish tag you’ve chosen, exactly?

确切地说,您选择的练习-完成标签的相关性是什么?

Then I have this list- names = (employee1, employee2, employee3, employee4, employee5)” What you have is a tuple which is different than a list in Python. Where are each of these employee# variables defined? Can you show us your code as a minimal reproducible example?

“然后我有这样一个列表-NAMES=(Emploe1,Emploe2,Emploe3,Emploe4,Emploe5)”您所拥有的是一个元组,它与Python中的列表不同。这些Employee#变量都是在哪里定义的?你能把你的代码作为一个最小的可重现的例子展示给我们吗?

You will not learn Python by guess-and-checking syntax. If you don't know the difference between () and [] and are simply throwing tokens at a wall to see what the interpreter will accept, then you're not going to get anywhere. Start at the basics, with a good Python tutorial. And then, if you have trouble, odds are you'll be able to identify the problem yourself rather than keyboard smashing in an attempt to appease the Python interpreter.

你不会通过猜测和检查语法来学习Python。如果你不知道()和[]之间的区别,只是简单地向墙上扔令牌,看看解释器会接受什么,那么你将一事无成。从基础开始,有一个好的Python教程。然后,如果您遇到麻烦,您很可能能够自己识别问题,而不是试图通过敲击键盘来安抚Python解释器。

From where did you ascertain that print(names[]) would accomplish what you seem to be attempting? Can you link the source docs on which you’re basing this assertion so that we might be able to shed some light on where your interpretation deviated from the syntax of the language?

您是从哪里确定指纹(名字[])会实现您似乎正在尝试的事情的?你能链接这个断言所基于的源文档吗?这样我们就可以阐明你的解释偏离了语言的语法。

Further, can you share what the expected user input is? A single digit? A string?

此外,您能分享一下预期的用户输入是什么吗?一位数?一根绳子?

优秀答案推荐

use a for loop with enumerate to generate and print a dictionary of options on-the-fly.

使用带有ENUMERATE的for循环动态生成和打印选项词典。


names = ('employee1', 'employee2', 'employee3', 'employee4', 'employee5')

options = {}
for i, name in enumerate(names, 1):
options[i] = name
print(f'{i}: {name}')
pick = int(input('Select Employee 1-5 to print their information:'))
if pick in options:
print(pick, options[pick])
else:
print(pick, 'not an option')


names = ("employee1", "employee2", "employee3", "employee4","employee5")

NAMES=(“Emploe1”,“Emploe2”,“Emploe3”,“Emploe4”,“Emploe5”)


First of all you need to define your str values using " ".

首先,您需要使用“”定义您的字符串值。


Second step is:

第二步是:


print(names[0])

Please note that, if you want to call value from tuple, you need to remeber about correct bracket

请注意,如果您想从元组调用值,您需要记住正确的括号


(names[0])

(名称[0])


Next step looks like this:

下一步看起来像这样:


pick = int(input('Select Employee 1-5 to print their information:'))
print(pick)
print(names[pick])

Also you need to rememeber, that input by default is using str, so you need to convert it into int.

另外,您需要记住,默认情况下输入使用的是字符串,因此需要将其转换为int。


Using int() function

使用int()函数



input() returns a string value. So you need to convert that to an integer to use it as an index into your employee list:

Int()返回一个字符串值。因此,您需要将其转换为整数,才能将其用作员工列表的索引:


pick = input('Select Employee 1-5 to print their information:') 
pick = int(pick)

Also, python tuples and lists are indexed starting at zero, not one, so you need to subtract one from the number they entered:

此外,从0而不是1开始对python元组和列表进行索引,因此您需要从它们输入的数字中减去1:


print(names[pick-1])

更多回答

Personally, I'd rather generate and print in separate steps: options = dict(enumerate(names, 1)); for i, name in options.items(): print(f'{i}: {name}'). That lets you move things around more easily later if needed.

就我个人而言,我更喜欢在不同的步骤中生成和打印:Options=dict(ENUMPERATE(NAMES,1));对于I,在options.Items():print(f‘{i}:{name}’)中命名。这使您可以在以后需要时更轻松地移动物品。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com