Homework No.5
Homework No.5
5)
Python Functions
Instructions:
1-Rename your file with your name and roll number (e.g., Haroon_25123455.py)
2-Do all your tasks in a single .py file.
3-This is your homework practice for the studied topics that can also help you in
your lab quizzes and assignments as well.
4-Submit your homework on Moodle within the specified deadline.
Task 1:
Write a function that continuously prompts the user to enter numbers until the sum of the
numbers entered reaches or exceeds 100. Skip adding any negative numbers to the sum and
ignore numbers greater than 50. Print the final sum. (use exactly the same function name
given below)
Task 2:
Write a python function, Inside the function Ask the user for an upper limit n using
a for loop and the range function to find and print all prime numbers from 2 to n. Skip even
numbers after checking 2.
Task 4:
Write a python function that takes a string as input and returns the string in reverse.
Implement this using both a for loop and a while loop.
Task 5:
Write a python function that generates a multiplication table (from 1 to 10) for a number
input by the user. Use the for loop and the range() function. Ask the user if they want the
table to start from 1 or another number, and adjust the range()accordingly.
Task 6:
Write a python function that prompts the user to input numbers continuously until they
enter a negative number. The function should sum only the even numbers entered and
ignore the odd numbers. Print the sum when the user enters a negative number.
Task 7:
Write a function to Ask the user for the number of terms they want in a Fibonacci sequence
and print the sequence using a for loop. Implement the sequence.
Task 8:
Write a function that picks a random number between 1 and 50. Allow the user to guess the
number, but limit the number of guesses to 5 using a for loop. After each incorrect guess, tell
the user if the number is higher or lower. If they guess correctly or run out of attempts,
print an appropriate message.
Task 9:
Write a python function that prompts the user for two numbers as the start and end of a
range. The function should find and print all numbers within this range that are both prime
and palindromic. A palindromic number is one that reads the same forwards and backwards
(e.g., 101). Ensure that your function efficiently checks for both conditions using loops and
the range() function.
● Input: Two numbers from the user representing the start and end of the range.
● Output: A list of numbers that are both prime and palindromic.
● fun range_palindrom():