Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 NameError: name 'countryDat' is not defined alien748 Unladen Swallow Posts: 2 Threads: 1 Joined: Aug 2017 Reputation: 0 Likes received: 0 #1 Aug-27-2017, 07:28 PM 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 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. ichabod801 Bunny Rabbit Posts: 4,231 Threads: 94 Joined: Sep 2016 Reputation: 271 Likes received: 1262 #2 Aug-27-2017, 08:31 PM (This post was last modified: Aug-27-2017, 08:31 PM by ichabod801. Edited 1 time in total. Edit Reason: Ran into a plyon ) This line is probably tripping you up: ```if x is country: ```The is operator is not the same as equals. It means that both variables point to the same place in memory, that they are not only equal, but that they are the exact same thing. Try: ```if x == country: ```instead. Also, it would work better to use a dictionary for that information. If you haven't studied those yet, look them up. I believe there's a tutorial on them in the tutorials section of this site. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures alien748 Unladen Swallow Posts: 2 Threads: 1 Joined: Aug 2017 Reputation: 0 Likes received: 0 #3 Aug-27-2017, 08:59 PM Thanks! I can't believe I spent hours trying to figure it out while the mistake was so small... ichabod801 Bunny Rabbit Posts: 4,231 Threads: 94 Joined: Sep 2016 Reputation: 271 Likes received: 1262 #4 Aug-27-2017, 09:04 PM (Aug-27-2017, 08:59 PM)alien748 Wrote: Thanks! I can't believe I spent hours trying to figure it out while the mistake was so small... Welcome to computer programming. nilamo likes this post Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post NameError: name 'edit' is not defined CabbageMan 5 264 Dec-17-2019, 04:48 PM Last Post: CabbageMan NameError 'odometer' not defined rfernandez15 1 185 Nov-25-2019, 09:57 PM Last Post: ichabod801 NameError: NameError: global name 'BPLInstruction' is not defined colt 7 480 Oct-27-2019, 07:49 AM Last Post: Larz60+ Function Annotation got NameError: name 'xxx' is not defined Lance 6 583 Oct-23-2019, 03:13 AM Last Post: Lance NameError: name 'processing' is not defined soli004 2 698 Sep-07-2019, 11:57 AM Last Post: soli004 NameError: name 'display' is not defined when running code on power bi beginner1 2 2,862 Jul-24-2019, 11:03 AM Last Post: beginner1 tkinter - "NameError: name 'frame' is not defined" Mocap 3 1,661 Jul-21-2019, 09:03 PM Last Post: Yoriz [split] NameError: name 'lo' is not defined when using input ? Cedmo 2 555 Jul-05-2019, 09:39 PM Last Post: Cedmo NameError: name 'lst' is not defined Jmekubo 2 1,027 Apr-27-2019, 12:34 PM Last Post: Jmekubo *** NameError: name '' is not defined adamG 1 1,069 Apr-08-2019, 01:21 PM Last Post: ichabod801

Forum Jump:

Users browsing this thread: 1 Guest(s)