This challenge is similar to an earlier one. Remember, though, I want you to practice! You'll probably want to use try a
Hello, I'm somewhat lost on what to do. Can someone please point me in the right directions.
# EXAMPLES def squared(num): try: for num in squared: if int(num) == True: return(num * num) except ValueError: return(num * len(num)) # squared(5) would return 25 # squared("2") would return 4 # squared("tim") would return "timtimtim"
Hey christiano! Yes your a bit off here, i had trouble with this one the first time too tho! To break down the problems:
First you define a function named squared, that takes a single parameter "num". This is good.
Then you "try" to do a for loop for the squared function, and assigned the value of each iteration to the variable num? Im not sure what your trying to accomplish there, and you dont even put anything inside of that for loop, so basiclly it does nothing.
Then you have an if statement that runs if turning the num variable into an integer is the same as "True", if thats the case you return num * num. Im assuming what your trying to do here, is check if the variable num can be turned into an integer. But this is what you should use the try block for, because if have something that cant be turned into an integer and you run the int() on it, it will spit a ValueError. So try to move this statement to your try block and remove the "== True".
Finally your except ValueError is good.
Try to redo this, if you still need more help, feel free to write back. Happy coding!
Hello, thank you that makes a more sense. Right now I'm getting: TypeError: can't multiply sequence by non-int of type 'str'
def squared(num): try: int(num) except ValueError: return(num * len(num)) else: return(num * num)
I guess i'm not converting the length of num to multiple by itself, but I'm not sure what the proper way would be?
Ahh, Thank you so much for your help!