0

i have trouble with the height value : when i enter Height of 1 or 25 (where Height must be higher than 1 and not higner than 23), it does print the error message but it also print the pyramid. What i am doing wrong, that the break doesn't work ? tks

Here is the error :

~/workspace/pset6/ $ python mario.py 
Height: 25
enter Height value between 1 and 23

  ##
 ###
####
etc.....

Here is the code:

import cs50


def main():
    print("Height: ", end="")
    Height = cs50.get_int()

    while True:
        if Height > 1 or Height < 23:
            print("enter Height value between 1 and 23")
        break



    for i in range (Height):
        print(" "*(Height-i-1), end="")
        print("#"*(i+2), end=")
        # print newline
        print()

if __name__ == '__main__':
    main()
1
while True:
    if Height > 1 or Height < 23:
        print("enter Height value between 1 and 23")
    break

the right way is:EDIT (you are right)

while True:
    print("enter Height value between 1 and 23")
    Height = cs50.get_int()
    if Height >= 0 or Height <= 23:
        break

You can delete the previous print and get_int, nor is a main function necessary for this case Note the indented is necessary to exit the loop when the height is correct. No, it is not necessary to declare the variables in python, we will simply name them when we want to use them, taking into account the rules of variable shading

7
  • well if i dont copy like this, it didn't work. So what is the correct way to right. : def main(): print("Height: ", end="") Height = cs50.get_int() while True: if Height < 1 or Height > 23: print("Height: ", end="") Height = cs50.get_int() break
    – Genevie
    Sep 2 '17 at 17:14
  • you can read my edit
    – MARS
    Sep 2 '17 at 17:21
  • thank u so much. ca u add the beginning, to see the relation with the ask for prompt and then while True value, because Ive seen some to write everything in the while True ?
    – Genevie
    Sep 2 '17 at 18:26
  • but how come u wirte the print before the condition ?
    – Genevie
    Sep 2 '17 at 18:27
  • ahahha it doesn,t work anymore:(
    – Genevie
    Sep 2 '17 at 18:30
0

i got it ! i had to add again the declaration of Height after the if condition. what i understand is that in python we have to declare the variable each time we use it compare to see that we could declare it once at the beginning and then use it during all the code. Please correct me if the answer is not right.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .