Python Forum
NameError: name 'countryDat' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'countryDat' is not defined
#1
Hey, so I'm new to Python and I attempted to practice what I learned from tutorials by making a program that gives you how many years you have to live based on life expectancy in a country, your age, and if you smoke or not. I use Python 3.6.2 in PyCharm.

def calc_life(age, country, smoke):
    '''
    I made a list of three countries and another with their life expectancy average. I also make a counter 'y'
    to use in the 'for x in countryList' loop.
    '''
    countryTime = [75, 85, 90]
    countryList = ["Mexico", "US", "Canada"]
    global countryDat, smokeDat
    y = 0

    for x in countryList:       #This loop gets the life expectancy of the country. The result is 'countryDat'
        if x is country:
            countryDat = countryTime[y]
        else:
            y = y + 1

    if smoke is "Yes":          #This loop is to assign a value to smokeDat depending if the person smokes
        smokeDat = -10
    elif smoke is "No":
        smokeDat = 0

    time = (countryDat - age) + smokeDat    #This just adds the previous results to get the remaning expected years of the person
    return time

#This just asks the user the info needed for the 'calc_life' funcion
print("How old are you?")
age = int(input())
print("In what country do you live? (Mexico, US, or Canada)")
country = input()
print("Do you smoke?")
smoke = input()

result = calc_life(age, country, smoke)

print(result)
I get the following when I run it:
How old are you?
33
In what country do you live? (Mexico, US, or Canada)
US
Do you smoke?
No
Traceback (most recent call last):
  File "C:/Users/josea/PycharmProjects/untitled/asdf.py", line 33, in <module>
    result = calc_life(age, country, smoke)
  File "C:/Users/josea/PycharmProjects/untitled/asdf.py", line 22, in calc_life
    time = (countryDat - age) + smokeDat    #This just adds the previous results to get the remaning expected years of the person
NameError: name 'countryDat' is not defined

Process finished with exit code 1
I've searched for solutions on the web and spent hours trying to find out what was wrong, but either they didn't work or solved the NameError and made another error appear. Thanks in advance.
Reply


Messages In This Thread
NameError: name 'countryDat' is not defined - by alien748 - Aug-27-2017, 07:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 289 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,110 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,898 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,338 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,518 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,924 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,358 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 14,990 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,393 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,893 Jul-26-2021, 04:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020