Where Did I Go Wrong with This Python Task?

0
5
Asked By CuriousCoder42 On

I'm working on a Python program with some specific requirements, and I'm feeling a bit lost. The task asks for a program to do the following: create a list of tuples, where each tuple contains a name and a set of numbers. Then, I need to filter the tuples to only include those with names containing the letter 'a' (either uppercase or lowercase), extract even numbers from the sets in those tuples, combine them into a unique collection without duplicates, multiply those unique numbers twice using a list (not a set), sort them, and finally display the result as a tuple. My teacher said this task is for beginner-level learners, but I'm having trouble figuring it out. Here's the code I've written:

```python
name = input('ur name: ')
age = int(input('ur age: '))
course = int(input('ur course: '))
univ = input('ur uni: ')

student = [
('adam', {1,2,3}),
('john', {4,5,6,7}),
('alan', {8,9,10,11})
]
student.append((name, {age, course}))

data = []
nums = set()

for i, z in student:
if 'a' in i:
data.append(i)
for n in z:
if n % 2 == 0:
nums.add(n)

for i in nums:
i *= 4
data.append(i)

print(data)
```

I don't know if I'm missing something or if I'm just overthinking it. I would appreciate hints on what I might be doing wrong, rather than a full solution. Thanks in advance!

3 Answers

Answered By Techy_Tommy93 On

Hey! Just a couple of thoughts. Firstly, don't forget to check for both uppercase and lowercase 'a' in the names. You may also want to include error handling for input conversions from strings to integers in your age and course inputs. You could use a try-except block for that! Lastly, for sorting, ensure you’re sorting a list right before printing it as a tuple.

Answered By PythonNinja_Tech On

From what I see, you might be misunderstanding requirement #5. It seems like they want you to create a new list with the multiplied values instead of just multiplying `i` in place. You could be looking for something like this:

```python
multiplied_nums = [n * 2 for n in nums]
```

Also, you need to sort your final result before displaying it as a tuple! Just make sure you adjust the variable to store that sorted output.

Answered By CodeGuru99 On

About the output: The instructions say to show the student's name, age, course, and university, but if you're just displaying the 'data' list, that won't give you the expected output. You should combine the relevant variables for your output clearly, maybe like this:

```python
student_info = (name, age, course, univ, sorted(data))
print(student_info)
```

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.