-1

So basically on my homework I need to use a FOR loop checking if an input is greater than ten but less than 20... I only know basic python, and this is stumping me.

I've used a while loop, I understand how to use that. The for loop is what gets me.

num1 = int(input("Enter a number greater than 10 and less than 20."))
while(num1<=10 and num1>=20):
    print("Invalid. Try again.")
    num1 = int(input("Enter a number greater than 10 and less than 20."))
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • @ppp Those answers don't use for loops – OneCricketeer Sep 15 '19 at 01:16
  • 2
    The question doesn't really make sense unless you are supposed to ask input a certain number of times... Otherwise, if you endlessly prompt, a while loop is appropriate – OneCricketeer Sep 15 '19 at 01:18
  • Why do you need a `for` loop? A `for` loop is used to repeat a process for a specific _range_ of values. For example, you want to ask the user to input a valid number, then if it is wrong, ask again, but only up to a maximum of 5 attempts, after which the program exits with an error. Unless there's a limit to the number of attempts or a specific number of valid inputs (you need 3 numbers), a `for` loop does not make sense here. – Gino Mempin Sep 15 '19 at 02:10
  • Either the problem is poorly designed, or you misinterpreted it. A `for` loop could be used but it looks unnecessary. – GZ0 Sep 15 '19 at 02:35

3 Answers3

0
num1 = int(input("Enter a number greater than 10 and less than 20."))
myNumbers=[11,12,13,14,15,16,17,18,19]
for _ in iter(int, 1):
  if num1 not in myNumbers:
    print("Invalid. Try again.")
    num1 = int(input("Enter a number greater than 10 and less than 20."))
leplandelaville
  • 186
  • 1
  • 9
0

You are using the correct logic, but not using correct Boolean statements.

num = 0
while ((num >= 10 and num <= 20) == False):
    num = int(input("Enter a Num: "))

print("Num in range")
BillyN
  • 185
  • 2
  • 10
-1

My guess is that maybe you could design a class for it with a for loop trial:

class Homework(object):
    def __init__(self):
        pass
    def range_validator(self, trial = 10):
        num1 = int(input("Enter a number greater than 10 and less than 20:\t"))
        for _ in range(0, trial):
            if num1 >= 10 and num1 <=20:
                print("Valid")
                exit()
            else:
                print("Invalid")
                self.range_validator()
                break


helper = """
Select an option:
    (1) => Enter a new integer
    (2) => Exit the program
"""
print(helper)
option = int(input("What would you like to do?\t"))


s = Homework()

while True:
    if (option == 1):
        s.range_validator()
    elif(option ==2):
        break
    else:
        break
Emma
  • 27,428
  • 11
  • 44
  • 69