-4

I have some code that I want to ask the user for a number between 1-100 and if they put a number between these it will print (Size: (input)) and break the loop, if however, they put in a number outside 1-100 it will print (Size: (input)), and proceed to re-ask them for a number, I have run into some problems though.

c=100
while c<100:
    c=input("Size: ")
    if c == 1 or 2 or 3:
        print ("Size: "+c)
        break
    else:
        print ("Size: "+c)
        print ("Invalid input. Try again.")
Bach
  • 6,145
  • 7
  • 36
  • 61
user2101517
  • 700
  • 3
  • 11
  • 22
  • 1
    "I have run into some problems" - what problems? – sashkello Apr 18 '13 at 02:58
  • The code doesn't work – user2101517 Apr 18 '13 at 03:00
  • 1
    When asking a question tell us HOW it doesn't work. You get error? It gives output you don't expect? What exactly is wrong? – sashkello Apr 18 '13 at 03:02
  • possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Veedrac Jun 12 '14 at 21:47
  • possible duplicate of [Why does \`a == b or c or d\` always evaluate to True?](http://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true) – glglgl Jun 13 '14 at 09:10

2 Answers2

3

This should do it.

c=input("Size: ")
while int(c)>100 or int(c)<1: 
    #Will repeat until the number is between 1 and 100, inclusively.
    #It will skip the loop if the value is between 1 and 100.

    print ("Size: "+c)
    print ("Invalid input. Try again.")
    c=input("Size: ")

#once the loop is completed, the code following the loop will run
print ("Size: "+c)
Patrick Poitras
  • 367
  • 1
  • 11
1

You never even enter your loop.

c=100
while c<100:

c is initiated at 100 and while checks if it is less than 100.

sashkello
  • 17,306
  • 24
  • 81
  • 109