Skip to content

[LABS] Ricardo error-handling#8

Open
Ricardo-Beato wants to merge 1 commit intoIronhack-data-bcn-oct-2023:mainfrom
Ricardo-Beato:main
Open

[LABS] Ricardo error-handling#8
Ricardo-Beato wants to merge 1 commit intoIronhack-data-bcn-oct-2023:mainfrom
Ricardo-Beato:main

Conversation

@Ricardo-Beato
Copy link
Copy Markdown

No description provided.

@bripollc
Copy link
Copy Markdown

Hello Ricardo:)

I'll leave you some little details but good lab in general! Gradually the things are getting rhythm:) Congratulations 💥

  • Calculate the square root of the first 100 numbers. Use sqrt as the name of the list.

I like the way you have used numpy! We will see it today:)

  • Find all of the numbers from 1-1000 that are divisible by 7. Use divisible_by_seven as the name of the list.
divisible_by_seven2=[i for i in range(1,1001)  if i%7 == 0]
print(divisible_by_seven2)

It asks you for the numbers divisible by 7 from 1-1000, in your range you specify range(0, 1001) so you include also the 0🫠 little confusão hehe (but your code is OK!!)

  • Remove all of the vowels in a string. Hint: make a list of the non-vowels. Use non_vowels as the name of the list.
vowels = "aeiouAEIOU"
non_vowels = [i for i in teststring if i.lower() not in vowels and i != ' ']
print(non_vowels)

Here is a more efficient and shorter way to achieve the same result!!!

  • 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}")

Your answer is correct and it works! I just wanted to let you know that the ZeroDivisionError exception is more specific to this particular error.

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

Idem that the previous exersice but with 'IndexError'.

I leave you the BONUS as well:

  • 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") 

Let's go for the next laaaaaaaab! 💥

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