0
print("this program will calculate the area")

input("[Press any key to start]")

width = int(input("enter width"))
if width < 0:
    print ("please chose a number between 0-1000")
    width = int(input("enter width"))

if width > 1000000:
    print ("please chose a number between 0-1000")
    width = int(input("enter width"))

height = int(input("Enter Height"))

area = width*height

print("The area is:",area)

Is there a way i can condense the code below for example fit them together so i won't have to write just about the same line of code except the less then and greater then statement twice.

width = int(input("enter width"))
if width < 0:
    print ("please chose a number between 0-1000")
    width = int(input("enter width"))

if width > 1000000:
    print ("please chose a number between 0-1000")
    width = int(input("enter width"))

I have tried

width = int(input("enter width"))
if width < 0 and > 10000:
    print ("please chose a number between 0-1000")
    width = int(input("enter width"))

But i get no love.

I also don't want to type

width = int(input("enter width"))

statement twice if it can be helped.

Thanks Ben

1
  • Nice work guys, Ty for your time. Commented Mar 8, 2013 at 0:10

6 Answers 6

7

There are a few ways to do it. The most explicit is this:

if width < 0 or width > 10000:

but my favourite is this:

if not 0 <= width <= 10000:
Sign up to request clarification or add additional context in comments.

1 Comment

Really you where all rite so thanks, but this one suited me best :)
3

You need a loop. Otherwise, the user can still enter an invalid value if they are persistent. The while statement combines a loop with a conditional - it keeps looping until the condition is broken.

width = -1
while width < 0 or width > 10000:
    width = int(input("enter width as a positive integer < 10000"))

Your use of the if statement in the original question is syntactically incorrect:

if width < 0 and > 10000:

You want:

if not (0 < width < 1000):
    ask_for_new_input()

or, in a more explicit way:

if width < 0 or width > 1000:
    ask_for_new_input()

Comments

0

The

if width < 0 and > 10000:

should read

if width < 0 or width > 10000:

Alternatively:

if not 0 <= width <= 10000:

Comments

0

You miss variable width

if width < 0 or width> 10000:

Comments

0

What if:

print("this program will calculate the area")

res = raw_input("[Press any key to start]")

def get_value(name):
    msg = "enter {0}".format(name)
    pMsg = "please choose a number between 0-1000"
    val = int(input(msg))
    while val not in xrange(1, 1001):
        print pMsg
        val = int(input(msg))
    return val


print("The area is: {0}".format(get_value('width') * get_value('height')))

Comments

0

You want to say

if width < 0 or width > 10000:
    print ("please chose a number between 0-1000")
    width = int(input("enter width"))

1 Comment

Nobody wants to say width < 0 or > 10000.

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.