0

I am new to learning python, and I only have limited programing experience, so I am sorry for asking such a basic question. I am doing an online python tutorial and wanted to take the program a bit further than the lesson requested to test out how far I could take it. The problem is that I have been hitting a roadblock. After reading a bunch of posts on here I have moved things around, changed variable names, used more functions, less functions, if else statements, and toyed around/ troubleshot, but have received all sorts of errors. I am going to paste where I currently am at below and would really appreciate if someone out there could help me finish up the program.

Here is what I would like for the program to do:

  • I would like the user to be prompted to enter a number.
  • I would like to make sure that the user entered a number, and if they don't I would like the program to give them an error and restart.
  • I would like that once the number is received the program squares that number and then prints the answer. (again thanks much for all the help)

HERE IS WHERE I AM AT:

n1 = raw_input('Please enter your desired number:')

def n2():

    n2 = n1.isnumeric()

    return n2

def square(n2):

    squared = n2**2
    print "%d squared is %d." % (n2, squared)
    return squared    

square(n2)
2
  • 1
    What kind number would you get only int or both int and float Commented Aug 21, 2015 at 5:44
  • I guess it could be either one.
    – fljlee
    Commented Aug 21, 2015 at 5:48

2 Answers 2

1
n2 = raw_input("number pls\n")
while not n2.isdigit():
      print("why did you do this\n")
      n2 = raw_input("number pls\n")

print int(n2)**2

Problems from your code: You need a control structure which repeatedly tries to do something until the right thing happens. This should make you think "while loop."

isnumeric isn't a method. but you had the right idea.

Naming all of your variables and functions n2 is not a good way to easily see what's going on..

If you want to add a construct for floats, you should write a function which takes a string in and returns true or false based on if it looks like a float! This is a good example for using functions to decompose the problem you're working with- you know you need to do something like

while not (n2.isdigit or isFloat(n2)):

There are some good suggestions here and elsewhere about how to do that :)

1
  • I will mess around with it later.
    – fljlee
    Commented Aug 21, 2015 at 6:24
0

You could check if you could convert into float if so print the square else print the error .To repeat this process while you get a number you could use while loop

def number_validator( numb ):    
    try:
        float( numb )
        return True
    except Exception:
        print "GIven input is not a valid number"
        return False

def square( n2 ):
    n2=float( n2 )
    squared = n2**2
    print "%s squared is %s." % ( n2 , squared )

    return squared    
n2 = raw_input( 'Please enter your desired number:' )
while not number_validator( n2 ):
    n2 = raw_input( 'Please enter your desired number:' )

square( n2 )

Output:

Please enter your desired number:wq
GIven input is not a valid number

Please enter your desired number:we
GIven input is not a valid number

Please enter your desired number:21.0
21.0 squared is 441.0.
1
  • I will mess around with it later, see if I can get everything to work
    – fljlee
    Commented Aug 21, 2015 at 6:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.