Skip to content

lab errors and list comprehension#21

Open
sarabenitezinglott wants to merge 1 commit intoIronhack-data-bcn-oct-2023:mainfrom
Ironhack-data23:main
Open

lab errors and list comprehension#21
sarabenitezinglott wants to merge 1 commit intoIronhack-data-bcn-oct-2023:mainfrom
Ironhack-data23:main

Conversation

@sarabenitezinglott
Copy link
Copy Markdown

No description provided.

@bripollc
Copy link
Copy Markdown

Hello Sara:)

Te dejo algunas cosillas pero buen lab en general!! Poco a poco la cosa va cogiendo ritmo:) Felicidades 💥

  • Convert the numbers of the following nested list to floats. Use floats as the name of the list.
flatten_list = [i for sublist in list_of_lists for i in sublist]
print(flatten_list)

Te dejo una manera de solucionarlo:) Tu approach no es del todo correcto.

  • Handle the exception thrown by the code below by using try and except blocks. (division by 0)
try:
    z = x/y
except ZeroDivisionError:
    print(f"I cannot divide a number by {y}")

Tu respuesta es correcta y funciona! Solo quería que supieras que la excepción ZeroDivisionError es más especifica para este error en concreto.

  • Handle the exception thrown by the code below by using try and except blocks. (index)

Idem que el ejercicio anterior pero con 'IndexError'.

Te dejo el BONUS también:

  • Bonus 1
 def square_cal():
    x = input("Insert an integer to square")
    integer=False
    while integer == False:
        try:
            x= int(x)
            integer == True
            return x**2
        except ValueError as error:
            x = input(f"{error}, please insert an valid integer to square")
square_cal()
  • Bonus 2
 results = list(set([num for div in range(2,10) for num in range(1,1001) if num%div == 0]))
print(results)
  • Bonus 3
 class NumSectionsError(Exception):
    pass
while True:
    try:
        Total_Marks = int(input("Enter Total Marks Scored: "))
        break
    except ValueError as error:
        print(error)
while True:
    try:
        Num_of_Sections = int(input("Enter Num of Sections: "))
        if Num_of_Sections > 2:
            raise NumSectionsError
        else:
            break
    except ValueError as error:
        print(error)
    except NumSectionsError:
        print("The number of sections can't be bigger than 2") 

A por el próximo laaaaaaab! 🙌🏼

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants