0

I am having issues regarding python. My issue is that my code always prints the first if statements return even if the user inputs a valid integer. I genuinely do not know the issue. I have to allow the user to input a value that is restricted to 1-10 and I have to be able to output either, "Please input an integer between 1-10" or "That's not even a number silly!". Could someone please help me and do more than just write the code, but explain it too?

value = input("Please input an integer: ")

def numValue(value):
    for value in range(-1000, 1000):
        if (value < 1, value > 10):
            return "Please input a number between 1-10."
        elif (value >= 1, value <= 10):
            return "Great choice!"

print (numValue(value))
  • You can't just join different conditions like `value < 1` and `value > 10` with a comma, you must use `and` or `or` for that. – Michael Butscher Sep 29 '17 at 02:48
  • Should the number be in between `1 to 10` or `-1000 to 1000`? – balki Sep 29 '17 at 02:49
  • Why do you even need the for loop? That for loop will keep running this 2000 times (approximately). You just need to check if value is in between 1 and 10 (inclusive). – Maelstrom Sep 29 '17 at 02:55
  • Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – wwii Sep 29 '17 at 03:32

1 Answers1

0
  • First you need to tell python that you are expecting an integer. This is done by wrapping int(..) around input
  • You have an unnecessary for loop with range. It is not needed
  • The syntax to check if a number is between a range is start < num < end or (<=) if you want the range to be inclusive
  • You don't to add the inverse check in elif, using else will automatically fallback if original condition fails

value = int(input("Please input an integer: "))

def numValue(value):
    if 1 <= value <= 10: 
        return "Great choice!"
    else:
        return "Please input a number between 1-10."

print (numValue(value))
balki
  • 26,394
  • 30
  • 105
  • 151