Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # while Loop to ask for a Start-Number,
- # restarts the Loop if User enters anything else than a Number
- while True:
- # trying to convert User-Input to a Number
- # and breaking the Loop on Success
- try:
- start_num = int(input("Enter a Start-Number: "))
- break
- # raising an Error if Input was not a Number
- # and restarting the Loop again
- except ValueError:
- print("Your Input was not a valid Number.")
- # same Loop as above, but with a check
- # if End-Number is bigger than Start-Number
- while True:
- # trying to convert as above
- try:
- end_num = int(input("Enter a End-Number: "))
- # If the Start-Number is bigger than the End-Number
- # L#notify the User about the Issue and restart the Loop
- if end_num <= start_num:
- print("Start-Number is bigger than End-Number")
- raise ValueError
- # break the Loop if everything is correct
- break
- except ValueError:
- print("Your Input was not a valid Number.")
- # List-Comprehention, very useful, watch a Tutorial about it
- # it is a Loop in 1 Line and transform it to a List
- # this will add every Number that has Modulo 0 to even
- even = [i for i in range(start_num, end_num) if i % 2 == 0 ]
- # this will add every Number that has Modulo 1 to odd
- odd = [i for i in range(start_num, end_num) if i % 2 == 1 ]
- # Printing the Results of the above Lists
- print(f"Sum of all even Numbers: {sum(even)}")
- print(f"Sum of all odd Numbers: {sum(odd)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement