简体   繁体   English

将值存储在列表、元组和集合中

[英]Storing values in a List, tuple and sets

ask you user 4 inputs.问你用户 4 输入。 Then, these must be stored in a list, tuple and, set.然后,这些必须存储在列表、元组和集合中。 If an input is invalid, the program will say which of the 3 (list, tuple, or set) cannot support what the user inputted and end the program.如果输入无效,程序将说明 3(列表、元组或集合)中的哪一个不支持用户输入的内容并结束程序。 If successful, the program will print the data that was stored.如果成功,程序将打印存储的数据。 This is the best i can do:这是我能做的最好的:

num_elements = []
tpl = ()
st = set()

list_length = int(input('Enter the range of the list: '))

try:
    for i in range(list_length):
    item = int(input('Enter the numbers: '))
    var = tpl + (tuple(item))
    num_elements.append(item)
    st.add(item)


print(str(num_elements))
print(tpl)
print(st)

except ValueError:
   print('Please enter a valid key')

I have fixed some issues with your code - your try/except syntax was wrong and your tuple initialization needed some change.我已经修复了您的代码的一些问题 - 您的 try/except 语法错误并且您的元组初始化需要一些更改。 Let me know if this works for your purposes.让我知道这是否适合您的目的。

num_elements = []
tpl = ()
st = set()

list_length = int(input('Enter the range of the list: '))

try:
    for i in range(list_length):
        item = int(input('Enter the numbers: '))
        tpl = tpl + (item,)
        num_elements.append(item)
        st.add(item)
except ValueError:
   print('Please enter a valid key')

print(num_elements)
print(tpl)
print(st)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM