Python Challenge


#1

Hey guys, I love to give computer geeks challenges every week. Heres one of them.
Create a program that inputs two numbers from the user. The first number will be an integer; the second number could have decimals. If the first number is 1, the output will be the square of the second number. If the first number is 2, the output will be the square root of the second number (both rounded to one decimal place).

Notes:

  1. You will need to import the Math library of functions by putting this line at the beginning of your program:

import math

  1. In order to input decimals, you should convert to input() value to a floating decimal number instead of an integer. Here is how the input line might look:
    number=float(input())

This is how it would look like
26%20PM


#2

Here’s my answer. python 3

import math

def square_or_squareroot(decider, input):
	if(decider == 1):
		return float("{:.1f}".format(math.pow(input, 2)))
	elif(decider == 2):
		return float("{:.1f}".format(math.sqrt(input)))
	else:
		return "Invalid input given"
		
print(square_or_squareroot(1,25))
print(square_or_squareroot(2,25))
print(square_or_squareroot(1,50))
print(square_or_squareroot(2,50))

#4

Line 4
return float("{:.if}".format(math.pow(input, 2)))
Invalid synthax ^
Line 5
return float("{:.if}".format(math.pow(input)))
Invalid synthax ^
Line 9
print(square_or_squareroot(1, 25))

ValueError: could not convert string to float: '{:,if ’


#5

Is this your homework ?


#6

No It isn’t, I was just letting you know, how the program you provided had errors


#7