在 python 2.7 中导入模块并获取回溯错误
Importing modules and getting traceback errors in python 2.7
我正在尝试将伪代码转换为工作 Python 文件。代码,无论多么小,都在其中导入了模块,我希望尽可能忠实于原始代码。当我转到 运行 脚本时,我在输入第一条数据后立即收到回溯错误。我最初包含了我试图从中翻译的伪代码,但我认为它造成了太多混乱,所以我删除了它。
#accepts ID number, name of agent, and the number of bedrooms
#outputs ID, name, and commission amount
#MY python code with modules.
#this was my way of trying to define variables
#so the main program would accept them.... instead of except them.
QUIT = 9999
commissionEarned = float
comm3 = float (100.00)
comm2 = float (75.00)
comm1 = float (55.00)
commStudio = float (30.00)
import getReady
while salesPersonID != 9999:
import detailLoop
import finish
模块 getReady()
salesPersonID = float (raw_input ('Enter salesperson ID or 9999 to quit: '))
模块 detailLoop()
salesPersonName = float (raw_input ('Enter name: '))
numBedrooms = float (raw_input ('Enter number of bedrooms: '))
if numBedrooms >= 3:
commissionEarned = comm3
elif numBedrooms == 2:
commissionEarned = comm2
elif numBedrooms == 1:
commissionEarned = comm1
else commissionEarned == commStudio
print salesPersonID, salesPersonName, commissionEarned
salesPersonID = float (raw_input ('Enter name: '))
模块完成()
print 'End of Program'
break
这是错误....我确定还有更多错误。
Python 2.7.9 (default, Dec 10 2014, 12:28:03) [MSC v.1500 64 bit (AMD64)]
on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
Enter salesperson ID or 9999 to quit: 1584
Traceback (most recent call last):
File "C:\Users\KirkandAngela\Desktop\Kirk\Find the bugs Ch4\DEBUG04-01.py",
line 8, in <module>
while salesPersonID != 9999:
NameError: name 'salesPersonID' is not defined
>>>
看来你可能不明白"import"的用法。
有两种访问变量(或其他)的方法:
1.
import getReady
# you can getReady.salesPersonID
2.
from getReady import salesPersonID
#you can salesPersonID
以我个人的观点,这些代码不是很pythonic。 ^_^
我正在尝试将伪代码转换为工作 Python 文件。代码,无论多么小,都在其中导入了模块,我希望尽可能忠实于原始代码。当我转到 运行 脚本时,我在输入第一条数据后立即收到回溯错误。我最初包含了我试图从中翻译的伪代码,但我认为它造成了太多混乱,所以我删除了它。
#accepts ID number, name of agent, and the number of bedrooms
#outputs ID, name, and commission amount
#MY python code with modules.
#this was my way of trying to define variables
#so the main program would accept them.... instead of except them.
QUIT = 9999
commissionEarned = float
comm3 = float (100.00)
comm2 = float (75.00)
comm1 = float (55.00)
commStudio = float (30.00)
import getReady
while salesPersonID != 9999:
import detailLoop
import finish
模块 getReady()
salesPersonID = float (raw_input ('Enter salesperson ID or 9999 to quit: '))
模块 detailLoop()
salesPersonName = float (raw_input ('Enter name: '))
numBedrooms = float (raw_input ('Enter number of bedrooms: '))
if numBedrooms >= 3:
commissionEarned = comm3
elif numBedrooms == 2:
commissionEarned = comm2
elif numBedrooms == 1:
commissionEarned = comm1
else commissionEarned == commStudio
print salesPersonID, salesPersonName, commissionEarned
salesPersonID = float (raw_input ('Enter name: '))
模块完成()
print 'End of Program'
break
这是错误....我确定还有更多错误。
Python 2.7.9 (default, Dec 10 2014, 12:28:03) [MSC v.1500 64 bit (AMD64)]
on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
Enter salesperson ID or 9999 to quit: 1584
Traceback (most recent call last):
File "C:\Users\KirkandAngela\Desktop\Kirk\Find the bugs Ch4\DEBUG04-01.py",
line 8, in <module>
while salesPersonID != 9999:
NameError: name 'salesPersonID' is not defined
>>>
看来你可能不明白"import"的用法。
有两种访问变量(或其他)的方法: 1.
import getReady
# you can getReady.salesPersonID
2.
from getReady import salesPersonID
#you can salesPersonID
以我个人的观点,这些代码不是很pythonic。 ^_^