1

I'm writing a script that is asking for user to input a number between 1-10, the script will then call the function Size and will return true if the number is greater or equal to 5 and false if not. However, I seem to have run into a bit of a wall, I get an error asking me to define x, this is my code currently.

def Size(x):
    if x >= 5:
        print("True")
    else:
        print("False")
    x = int(input("Please enter a number greater than 5"))
Size(x)
1
  • check your indentation... Commented Dec 8, 2020 at 6:19

4 Answers 4

3

You declared user input x in the Size function. X should be an outside size function

def Size(x):
    if x >= 5:
        print("True")
    else:
        print("False")


x = int(input("Please enter a number greater than 5"))
Size(x)

Or if you want to take user input in Size function then initialize x in Size function and pass just Size function then follow the second answer

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

2 Comments

HI toRex, no offense here, would like to mention here, my answer was already posted before your edit on your 2nd approach, thought to make it clear here, cheers :)
@RavinderSingh13 Sorry, I was editing that time. after modifying my code I saw your answer. Edited my answer :)
3

You were close, you need to correct 3 things in your code.

  • Ask user for input before doing checks in your function.
  • Remove variable passed in your user defined function.
  • You need to change name of your function for safer side to make it more meaningful, so I changed it to funcSize.

#!/usr/bin/python3
def funcSize():
    x = int(input("Please enter a number greater than 5: "))
    if x >= 5:
        print("True")
    else:
        print("False")

funcSize()

Comments

0

Define x outside the function. Rest of the code is correct.

Comments

0
def Size(x):
    if x >= 5:
        print("True")
    else:
        print("False")
    x = int(input("Please enter a number greater than 5"))
Size(x)

The error occured because I didn't fix my indentation after the false statement.

Comments

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.