Home Python 'height' is not defined
Reply: 2

Python 'height' is not defined

Jakob Hansen
1#
Jakob Hansen Published in 2017-11-14 17:37:57Z

Python is saying that the name 'height' is not defined, and I have no idea why that is, as according to my logic, I return the variable height, so I would be able to access it within my for loop?

Can someone point me in the right direction? Thanks. Edit: the get_int() is a function inside the cs50 library.

import cs50

def main():

    print("Enter a number between 0 and 26: ", end="")

    i = get_height("Enter height: ", end="")

def get_height():
    while True:
        height = get_int()
        if height <= 0 or height >= 23:
            break
    return height


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


if __name__ == "__main__":
    main()
Rafael Quintela
2#
Rafael Quintela Reply to 2017-11-14 17:57:33Z
def get_int():
    tx = input("Enter height: ")
    if tx.isdigit():
        return int(tx)
    return None

def get_height():
    while True:
        height = get_int()
        if height and height > 0 and height < 26:
            return height


if __name__ == "__main__":
    print("Enter a number between 0 and 26: ")

    height = get_height()
    for i in range(height):
        print(" " * (height - i), end='')
        print("#" * (i + 2), end='')
        print("")
  1. get_int doesn't exists
  2. height was out of scope
  3. height on the while loop was not visible from the def scope
  4. get_height get no param
  5. for in range was out of the main scope
wpercy
3#
wpercy Reply to 2017-11-14 17:52:55Z

First, off you need to use hight = raw_input("What is your hight") to get your height. You can then use int(height) to use it as an integer. Secondly, you need to have it be top-down processing.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.322293 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO