2
def defineType(riskCode):
 if riskCode == 1:
 riskType = High
 else:
 if riskCode == 2:
 riskType = Moderate
 else:
 if riskCode == 3:
 riskType = Low
 else:
 if RiskCode == 4:
 riskType = No

This is part of a program I'm working on for a class...

# Global Variables
custName = input('Please enter your name: ')
custAge = int(input('Please enter your age: '))
custViolation = int(input('Please enter the number of violations: '))
riskCode = 0
estimatePrice = 0
riskType = none

These are my global variable...

Traceback (most recent call last):
 File "C:\Users\Alli\Downloads\HwyRobbery.py", line 13, in <module>
 riskType = none
NameError: name 'none' is not defined

This is the error that I keep getting with different variations depending on what changes I make to try to solve the problem

Any help would be greatly appreciated!!!

Thanks! Alli

joaquin
86k31 gold badges146 silver badges155 bronze badges
asked Jun 17, 2011 at 15:08
0

6 Answers 6

16

It's None not none. Python is case sensitive.

answered Jun 17, 2011 at 15:10
Sign up to request clarification or add additional context in comments.

Comments

8

The initial problem I see is that none is not a built-in value in Python. None is though.

Also, the tower of nested if statements in the first function is unnecessary. Flatten that!

def defineType(riskCode):
 if riskCode == 1:
 riskType = High
 elif riskCode == 2:
 riskType = Moderate
 elif riskCode == 3:
 riskType = Low
 elif RiskCode == 4:
 riskType = No

It's not an error, but it's much more readable, no? (Easier to type too :)

Also, I take it that High, Moderate, Low, and No are defined elsewhere in your code? If you're using them as enumerated constants (i.e. as a set of unique integers whose values do not change), HIGH, MODERATE, LOW, and NO would be more idiomatic.

And in fact, if you did that, the whole function could be done away with like so:

HIGH = 1
MODERATE = 2
LOW = 3
NO = 4

Now you don't need to map them at all; and you can just assign riskCode to riskType, although that may be a bit redundant now!

riskType = riskCode

If you want to get strings from risk codes, try the second dictionary from Artsiom Rudzenka's answer.

Finally... I would suggest putting these codes in their own namespace:

class Risk(object):
 HIGH = 1
 MODERATE = 2
 LOW = 3
 NO = 4

You can then reference them like so:

if riskCode == Risk.HIGH: do_something()

Now you are free to use short words like NO for other things.

answered Jun 17, 2011 at 15:11

6 Comments

Also (sorry for the swarm of verbiage!) see jlargent's answer if you're trying to modify riskType as a global within your function.
I so appreciate your help, after doing what you suggested with changing none to None, and using the elif instead of if... i get thisTraceback (most recent call last): File "C:\Users\Alli\Downloads\HwyRobbery.py", line 79, in <module> main() File "C:\Users\Alli\Downloads\HwyRobbery.py", line 16, in main assignRisk() File "C:\Users\Alli\Downloads\HwyRobbery.py", line 37, in assignRisk defineType(riskCode) File "C:\Users\Alli\Downloads\HwyRobbery.py", line 70, in defineType riskType = Moderate NameError: global name 'Moderate' is not defined
Traceback (most recent call last): File "C:\Users\Alli\Downloads\HwyRobbery.py", line 79, in <module> main() File "C:\Users\Alli\Downloads\HwyRobbery.py", line 16, in main assignRisk() File "C:\Users\Alli\Downloads\HwyRobbery.py", line 37, in assignRisk defineType(riskCode) File "C:\Users\Alli\Downloads\HwyRobbery.py", line 70, in defineType riskType = Moderate NameError: global name 'Moderate' is not defined
And yes... I'm new... this is my 1st class, and I got the whole program to work, except the risk code part... and I'm about to pull my hair out!
Sorry, as I tried to explain, Moderate is a variable name in your code, so you have to define it! Or, if you want it to be a string, you have to put it on quotation marks.
|
3

No need to use function to use function for risks mapping you can simply use the following:

riskmapping = {1:High, 2:Moderate, 3:Low, 4:No}

But in case if High, ...No is not a instances or variables, but just a strings than:

riskmapping = {1:'High', 2:'Moderate', 3:'Low', 4:'No'}

Usage:

highRisk = riskmapping[1]

Or

if riskmapping[currentRiskCode]:
 #do smth
answered Jun 17, 2011 at 15:23

4 Comments

NameError: name 'High' is not defined
I'm guessing that Alli OGrady is new to python (sorry if not!) so I think you should show usage.
Actually i have added my code based on OP code, so i suppose he has definition for this variables, i only give the short version of his code.
def main(): assignRisk() calculateIns() defineType() if custAge < 16 or custAge > 105: print('Invalid Entry') elif custViolation < 0: print('Invalid Entry') else: print(custName, ', as a ', riskType, ' risk driver, your insurance will cost $', estimatePrice, '.00.') def defineType(): global riskType if riskCode == 1: riskType = High elif riskCode == 2: riskType = Moderate elif riskCode == 3: riskType = Low elif riskCode == 4: riskType = No
1

Apart from none, my contribution to simplicity:

riskCodes = {1: 'High', 2: 'Moderate', 3: 'Low', 4: 'No'}
def defineType(riskCode):
 riskType = riskCodes[riskCode]
answered Jun 17, 2011 at 15:24

Comments

1

If you are modifying global variables in a function other than were it is declared don't you have to use the global keyword.


def defineType(riskCode):
 global riskType
 if riskCode == 1:
 riskType = High
answered Jun 17, 2011 at 15:27

1 Comment

Moving from code that doesn't work at all to code that's very badly designed isn't necessarily a step in the right direction. 95% of the time, the global keyword means you're doing something horribly, horribly wrong.
1

Setting riskType inside your function is local to that function and will not affect the value in the global. You need to tell your function to use the global by putting global riskType before code that tries to modify it.

None should be capitalized.

I believe you meant to set riskType to a string and therefore those values should be quoted.

Python has an elif for else if.

The corrected code:

# Global Variables
riskCode = 0
estimatePrice = 0
riskType = None
def defineType(riskCode):
 global riskType
 if riskCode == 1:
 riskType = "High"
 elif riskCode == 2:
 riskType = "Moderate"
 elif riskCode == 3:
 riskType = "Low"
 elif RiskCode == 4:
 riskType = "No"
answered Jun 17, 2011 at 15:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.