Python if语句

Ben*_*eno 0 python

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)
Run Code Online (Sandbox Code Playgroud)

有没有办法可以压缩下面的代码,例如将它们放在一起,这样我就不必编写相同的代码行,除了那么少,然后更大的语句两次.

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"))
Run Code Online (Sandbox Code Playgroud)

我试过了

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

但我没有爱.

我也不想打字

width = int(input("enter width"))
Run Code Online (Sandbox Code Playgroud)

声明两次,如果它可以帮助.

谢谢Ben

Dan*_*man 7

有几种方法可以做到这一点.最明确的是:

if width < 0 or width > 10000:
Run Code Online (Sandbox Code Playgroud)

但我最喜欢的是:

if not 0 <= width <= 10000:
Run Code Online (Sandbox Code Playgroud)