Python Challenge


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).


  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:

This is how it would look like


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)))
		return "Invalid input given"


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 ’


Is this your homework ?


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