0

I want the code not to take the input when it is more than 23 or less than 0. It does reject negative values but still values bigger than 23.

height=int(input("select height: "))


while height<0 and height>23:
        print("please give input between 1 and 22")


for i in range(height):
    print(" "*(height-i)+"#"*(i+1))

I googled some stuff and kinda try to understand with trial and error but I couldn't.

 for i in range(height):
    print(" "*(height-i)+"#"*(i+1))
myradio
  • 1,703
  • 1
  • 15
  • 25

3 Answers3

1

The current code checks for if height is simultaneously less than 0 and greater than 23. Instead, try:

while height < 0 or height > 23:
  height = int(input("input height: "))#using input as well, to get new working value

As for how the last lines work: they create an ASCII pyramid of "#"s with equal height and width.

  • the first line for i in range(height) is just for how many rows there are.
  • the second line
    • prints empty spaces - " "*(height-i): for example if it is the 1st row of a 6 height pyramid, will print 5 spaces; if it is the 2nd row, will print 4 spaces. The multiplication just prints that character x times.
    • prints "#"s - "#"*(i+1). This works just like the above, but comes after the spaces, and in inverse.
ofthegoats
  • 295
  • 4
  • 10
0
while height<0 and height>23:
        print("please give input between 1 and 22")

You should be using if instead of while

for i in range(height):
    print(" "*(height-i)+"#"*(i+1))

The first line describes how many times the loop will execute Second how many times the string will be printed in each line.

Nidhin Bose J.
  • 1,092
  • 15
  • 28
0

try this

while height <= 0 or height >= 23:
    print("please give input between 1 and 22")
    height = int(input("select height: "))

while works when the value is true, and the number is simultaneously less than 0 and more than 23 cannot to be, use 'or' not 'and'

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Alex Burkov
  • 161
  • 1
  • 5