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
6 Answers 6
It's None not none. Python is case sensitive.
Comments
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.
6 Comments
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.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
4 Comments
Apart from none, my contribution to simplicity:
riskCodes = {1: 'High', 2: 'Moderate', 3: 'Low', 4: 'No'}
def defineType(riskCode):
riskType = riskCodes[riskCode]
Comments
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
1 Comment
global keyword means you're doing something horribly, horribly wrong.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"