In: Computer Science
python practice!
1. Create a function that takes a user choice and one number as parameters and returns the operation result.
-Square: print the number square
-Sqrt: print the square root of the number
-Reverse: reverse the sign of the number (pos or neg) and print it
Note: Detect invalid choices and throw an error message – Number can be anything.
2. Create a function that takes a user choice and two numbers (start and end) as parameters.
For example, two numbers can be 5 and 40.
-Evens: print the even numbers between the two numbers, start and end.
-Odds: print the odd numbers between the two numbers, start and end.
Hint: You will have to use a range function with the numbers start and end.
Solution
1)
Code
def myfunction( number ):
square=number*number
squareroot=number**0.5
reverse=-number
print("Square of the number: ", square)
print("Square root of the number: ", squareroot)
print("After reversing sign: ", reverse)
return
try:
number = int(input("Please enter a number: "))
myfunction( number )
except:
print("Invlid entry")
Screenshot
Output
---
Program 2
Code
def myfunction( start,end ):
print("Odd numbers between",start,"to",end)
for num in range(start+1, end):
if num % 2 != 0:
print(num, end = " ")
print("\nEven numbers between",start,"to",end)
for num in range(start+1, end):
if num % 2 == 0:
print(num, end = " ")
return
start = int(input("Please enter the starting number: "))
end = int(input("Please enter the end number: "))
myfunction( start,end )
Screenshpt
Output