-6

The Task

Write a program based on the following requirements:

  1. The list must contain several tuples - each tuple should include:
  • one name
  • one set consisting of numbers
  1. Select only the tuples where the name contains the letter "a" (uppercase or lowercase).

  2. From the sets inside the selected tuples, extract only the even numbers.

  3. Combine all extracted even numbers into a unique collection (no duplicates).

  4. Multiply the resulting unique numbers twice, but:

  • do this using a list, not a set.
  1. Sort the final values.

  2. Display the result as a tuple.

Output: It prints the student's name, age, course, and university as entered by the user.


Either there is a mistake somewhere in the question or I'm too dumb. This task came with the midterm test I took yesterday. "There is nothing complicated and I made it only for beginner level python learners" - as my teacher said (yeah sure no doubt).

It could be better if you guys give me hints and point out where am I going wrong instead of giving out the whole thing. I would have just gone to ask chatgpt, but I feel like I'm near solving it so yeah that is why I'm here.

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)
New contributor
workercat is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • 2
    Don't link images containing text. Copy'n'paste the relevant parts of the text instead. Also, pick a subject line thata conveys information about your problem. As a new user here, please start with the tour and read How to Ask. Commented yesterday
  • The Output: section of the assignment does not make any sense because you are being asked to output something that has nothing to do with what's computed in Steps 1 - 7. Commented yesterday
  • You are asked to select names that have "a" or "A" but you are only selecting names with "a". The even numbers you are selecting and multiplying by 4 are being added to data which has names in it. If we only look at steps 1 - 7, you should not be displaying names but rathe just the numbers sorted and converted to a tuple. You are not doing that. Commented yesterday

1 Answer 1

0

My review will not be overly pythonic, as I don't really use it. Nevertheless...

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

Use proper English, especially for a uni assignment.

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

Minor nitpick, but it should be students not student as this is a collection.

student.append((name, {age, course}))

Not sure if your input should go into the list?

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)

Pick better variable names. i should be name and z should be numbers. The code is much more readable that way:

names = []
unique_numbers = set()
for number, name in students:
  if 'a' in name:
    names.append(name)
    for n in numbers:
      if n % 2 == 0:
        unique_numbers.add(n)

You're only checking for a lowercase a, but the requirement says uppercase and lowercase. You can also make this code more concise using list comprehension

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

You're appending this to data, but data already contains the students' names. This is a bit confusing to me. Also the requirement is a bit unclear to me. It says you should multiply the resulting numbers twice. But it does not specify with what. Initially I understood you should calculate the product of them, but then the twice doesn't make sense. In any case, you only multiply each number once. You would need a second loop.

print(data)

You do not sort the final values as per the requirement.

These are the main things that came to mind. The Output requirement is also not really clear to me.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.