1

I am trying to write a code for squaring the user input number in Python. I've created function my1() ...

What I want to do is to make Python to take user input of a number and square it but if user added no value it gives a print statement and by default give the square of a default number for e.g 2

Here is what I've tried so far

def my1(a=4):
 if my1() is None:
    print('You have not entered anything')
 else:
    b=a**2
    print (b)

my1(input("Enter a Number"))
jpp
  • 159,742
  • 34
  • 281
  • 339

4 Answers4

2

This is a better solution:

def my1(a=4):
    if not a:
        return 'You have not entered anything'
    else:
        try:
            return int(a)**2
        except ValueError:
            return 'Invalid input provided'

my1(input("Enter a Number"))

Explanation

  • Have your function return values, instead of simply printing. This is good practice.
  • Use if not a to test if your string is empty. This is a Pythonic idiom.
  • Convert your input string to numeric data, e.g. via int.
  • Catch ValueError and return an appropriate message in case the user input is invalid.
jpp
  • 159,742
  • 34
  • 281
  • 339
0

In your second line, it should be if a is None:

I think what you want to do is something like the following:

def m1(user_input=None):
    if user_input is None or isinstance(user_input, int):
        print("Input error!")
        return 4
    else:
        return int(user_input)**2
print(my1(input("Input a number")))
Justin
  • 9
  • 6
  • Instead of `type(a) != int`, I would have done `isinstance(a, int)`. – IMCoins Mar 14 '18 at 13:13
  • Thanks for the comment, is there an actual benefit for doing the other way? – Justin Mar 14 '18 at 13:16
  • [From this thread](https://stackoverflow.com/questions/1549801/what-are-the-differences-between-type-and-isinstance), in short, and I quote it : `It's not that isinstance is good, mind you—it's just less bad than checking equality of types`. The Pythonic way to handle this case would `try/except` statement. – IMCoins Mar 14 '18 at 13:20
0

You're getting an infinite loop by calling my1() within my1(). I would make the following edits:

def my1(a):
    if a is '':
        print('You have not entered anything')
    else:
        b=int(a)**2
        print (b)

my1(input("Enter a Number"))
Connor John
  • 433
  • 2
  • 8
0

When I read your code, I can see that you are very confused about what you are writing. Try to organize your mind around the tasks you'll need to perform. Here, you want to :

  1. Receive your user inputs.
  2. Compute the data.
  3. Print accordingly.

First, take your input.

user_choice = input("Enter a number :")

Then, compute the data you received.

my1(user_choice)

You want your function, as of now, to print an error message if your type data is not good, else print the squared number.

def my1(user_choice): # Always give meaning to the name of your variables.
    if not user_choice:
        print 'Error'
    else:
        print user_choice ** 2

Here, you are basically saying "If my user_choice doesn't exists...". Meaning it equals False (it is a bit more complicated than this, but in short, you need to remember this). An empty string doesn't contain anything for instance. The other choice, else, is if you handled your error case, then your input must be right, so you compute your data accordingly.

IMCoins
  • 3,149
  • 1
  • 10
  • 25