Jump to content

variable not defined

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Dorgon

Dorgon

    Newbie

  • Members
  • PipPip
  • 24 posts
good morning,

i've started a study in programming in the Netherlands. but we don't get realy programming lessons.
so i started programming my self. a friend of my advided me to start with a east langage.
thats why i use python. but now i have a little problem.
a variable is not defined, i am a beginner, so i don't know what to do.

tips are welcome :D

here is my script:

import random

import time


def displayIntro():

    print('Welcome to Williams BMI calculator.')

    time.sleep(1)

    print('This programm wil calculate your BMI for you.')

    time.sleep(1)

    print('BMI means Body Mass Index, it shows you of your are on a healthy weight')

    print()



def enterInformation():    

    print('pleace enter your name')

    myName = input()

    print('Hello ' + myName + ' , Lets get started.')

    print()

    print('First typ in your weight in kg pleace.')

    weight = int()

    weight = input()

    print('Okay, now typ in your length in cm pleace.')

    length = int()

    length = input()


    calculateBMI()


def calculateBMI():

    BMI = weight / (length*length)


    if BMI < 20:

        print('Your BMI is too low, you should gain some weight.')

    if BMI > 25:

        print('Your BMI is too high, you should loose some weight.')

    else:

        print('Your BMI is perfect, keep this weight and you will live a happy life.')


displayIntro()


calculateAgain = 'yes'

while calculateAgain == 'yes' or calculateAgain == 'y':


    enterInformation()


    print('Do you want to calculate your BMI again? (yes or no)')

    calculateAgain = input()



the output/ errors are:

>>>
Welcome to Williams BMI calculator.
This programm wil calculate your BMI for you.
BMI means Body Mass Index, it shows you of your are on a healthy weight

pleace enter your name
William
Hello William , Lets get started.

First typ in your weight in kg pleace.
80
Okay, now typ in your length in cm pleace.
185
Traceback (most recent call last):
File "D:\python apps\BMI calculator.py.py", line 42, in <module>
enterInformation()
File "D:\python apps\BMI calculator.py.py", line 25, in enterInformation
calculateBMI()
File "D:\python apps\BMI calculator.py.py", line 28, in calculateBMI
BMI = weight / (length*length)
NameError: global name 'weight' is not defined
>>>

Edited by Dorgon, 11 November 2010 - 05:09 AM.


#2
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Please update your question using tag {CODE}your code here{/CODE} (replace { with [ and { with ]) to format your code. For now it's impossible to read your code, because spaces are lost.

#3
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
weight is local variable of enter_information() function. You can not use this variable outside function. If you want that variable to be globally accessible, you can use global class pattern: A Globals Class pattern*for*Python « Python Conquers The Universe , but the best way is to pass weight and height to calculate_bmi() function. I rewrote your script to follow PEP-8 ( PEP 8 -- Style Guide for Python Code ), and changed input() function to raw_input(), because input() is not safe:

Quote

This function is not safe from user errors! It expects a valid Python expression as input; if the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation. (On the other hand, sometimes this is exactly what you need when writing a quick script for expert use.)
So my variant of your script:
import time



class mem: pass



def display_intro():

    print('Welcome to Williams BMI calculator.')

    time.sleep(1)


    print('This programm wil calculate your BMI for you.')

    time.sleep(1)


    print('BMI means Body Mass Index, it shows you of your are on a healthy weight\n')



def enter_information():

    mem.name = raw_input('Pleace enter your name: ')


    print('Hello %s, Lets get started.\n\n' % mem.name)


    mem.weight = int(raw_input('First please enter your weight in kg: '))

    mem.height = int(raw_input('Okay, now please enter your height in cm: '))


    calculate_bmi()



def calculate_bmi():

    bmi = mem.weight / (mem.height * mem.height)


    if bmi < 20:

        print('Your BMI is too low, you should gain some weight.')

    elif bmi > 25:

        print('Your BMI is too high, you should loose some weight.')

    else:

        print('Your BMI is perfect, keep this weight and you will live a happy life.')



display_intro()


while True:

    enter_information()


    answer = raw_input('Do you want to calculate your BMI again? (yes or no) ')

    if not answer in ['yes', 'y']:

        break

To further improve your script, you should remove mem class and rewrite calculate_bmi() as this:
calculate_bmi(weight, height):

Edited by Vladimir, 13 November 2010 - 12:45 PM.


#4
Dorgon

Dorgon

    Newbie

  • Members
  • PipPip
  • 24 posts
thanks, you are using some things that i don't know yet. but i will learn that in the future.

#5
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
You are welcome to ask questions.

#6
Dorgon

Dorgon

    Newbie

  • Members
  • PipPip
  • 24 posts
hey, sorry for my late response. but your script does say:


Traceback (most recent call last):

  File "D:\python apps\bmi cal improve", line 42, in <module>

    enter_information()

  File "D:\python apps\bmi cal improve", line 18, in enter_information

    mem.name = raw_input('Pleace enter your name: ')

NameError: global name 'raw_input' is not defined

>>> 



#7
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
If you are using Python 3, then you should use input() instead of raw_input(), because raw_input() was renamed to input() in Python 3. You can check this PEP for more info: PEP 3111 -- Simple input built-in in Python 3000

#8
Dorgon

Dorgon

    Newbie

  • Members
  • PipPip
  • 24 posts
ah ok, yes i am using python 3.

thank you :D