-1
def total():
    n = prompt()
    answer = (n-2)*180
    print ("The polygon has", answer, "sides.")

 def prompt():
     x = raw_input("Type number: ")
     return x 

I'm trying to get n which is equal to the output of prompt to be an integer so that math can be done on it. How would I do this?

poke
  • 369,085
  • 72
  • 557
  • 602
  • 1
    `n = int(prompt())`, or `return int(x)` (depending on where you want the conversion to happen) – poke Mar 02 '15 at 20:05

1 Answers1

0

raw_input returns a string, so you cannot use it for any calculations. If you want to have an integer instead, you can use the int function to convert the value. You can do that either within your prompt function, or later in the calling function (although it would make more sense to have a function that asks the user for a number return one):

def prompt ():
    x = raw_input("Type a number: ")
    return int(x)

Note, that int() may raise a ValueError for any user entered value that is not a valid integer. In that case, you should catch the exception and prompt the user again to correct the input. See this question on how that would work.

Community
  • 1
  • 1
poke
  • 369,085
  • 72
  • 557
  • 602