Why doesn't my squaring function run?

前端 未结 1 696
抹茶落季
抹茶落季 2021-01-29 16:01

I decided to make a program that would square a number just for fun. Using an online compiler, I entered my code and from what I saw there were no errors; it wouldn\'t run it wo

相关标签:
1条回答
  • 2021-01-29 16:15

    Do make sure you also call your function:

    def square():
        # your function body here
    
    square()
    

    But in your function, you are ignoring the result of your calculation here:

    number*number
    

    Assign that result to something:

    answer = number * number
    print "Your answer is..."  
    print answer  
    

    You don't have a number, however. raw_input() returns a string, so you want to convert that to a number first:

    number = int(number)
    

    This assumes that the user actually entered something that can be converted to an integer; digits only, plus perhaps some whitespace and a + or - at the start. If you wanted to handle user errors gracefully here, take a look at Asking the user for input until they give a valid response for more advanced options.

    0 讨论(0)
提交回复
热议问题