1

so I'm very noobish, I got this python code that I found somewhere in my folders, because I started learning python a while ago, and I need this code for class today. Thing is, it doesn't print anything, it just indicates that there's no problem with it. Can you help me? I need to sc the code and sc the output, if you can guide me to what line of code im missing or anything really.. thanks

def square(n):
    word = int(raw_input('Enter number here: '))
    if len(word) > 0:
        squared = n ** 2
        print ("%d squared is %d" %(n,squared))
8
  • 1
    Are you somewhere calling this function you have defined? Commented Nov 20, 2018 at 8:57
  • Uhm, no? How do i do that :x? print (square)? Commented Nov 20, 2018 at 8:59
  • this will generate error I guess. object of type 'int' has no len(). if len(word) > 0: Commented Nov 20, 2018 at 9:00
  • That's true, I removed the int, uhm, I guess I'll change the len(word) to if number > 0 or something like that? Would that be more correct? Commented Nov 20, 2018 at 9:01
  • 1
    Can you explain what the significance of the variable "word" here? Commented Nov 20, 2018 at 9:06

2 Answers 2

1

First of all, using Python 3, you need to replace raw_input with input. Secondly and most importantly, integer does not work with len function and you should compare your integer directly. To handle potential type mismatch, use following code (you can put it in a loop or do any other modifications)

def square():
    n = input('Enter number here: ')
    try:
        n = int(n)
    except TypeError:
        print("Input is not a number")
    else:
        if word > 0:
            squared = n ** 2
            print ("%d squared is %d" %(n,squared))

# Let's call the function
square()

By the way, I think calling integer variable word is not very self-descriptive.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the code, it's waaay better than the one I used, thing is it still doesn't allow me to input a number when I run it. When I run it on Visual Studio, it opens the cmd prompt, says "Enter a key to continue" , any key pressed closes the window. I need to SC the output of the code too.
I tried Online Python Compilers, doesn't give me an answer. Can you suggest me what compiler to use? Or what do you use, so this can give me an output? @Michal Polovka
One important thing - do you call your square function anywhere in the code?
No, how do I call it? Just print square?
Marked as answer. I upvoted and it was "recorded" but not counted because I'm new to the forums.
0

I think this will work:

def square(n):
    number = int(input('Enter number here: '))
    if number > 0:
        squared = n ** 2
        print ("%d squared is %d" %(n,squared))

3 Comments

integers don't have len()
yes you are correct. But in this context the variable "word" is used as string.
This is still very wrong, the variable number is not squared, the numerical argument is

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.