2015-02-11 81 views
0

我試圖將一個僞代碼翻譯成一個可用的Python文件。這些代碼雖然很小,但在其中已經導入了模塊,我希望儘可能保持原樣。當我運行腳本時,在輸入第一個數據之後,會立即發現回溯錯誤。我原來包含了我試圖翻譯的僞碼,但我認爲它導致了很多混亂,因此我將其刪除了。導入模塊並獲取python 2.7中的回溯錯誤

#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 

這是ERROR .... IM SURE還有更多。

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 
>>> 
+0

我對此很新。你是說我不能將模塊中的函數調用到程序中? – 2015-02-11 05:27:19

回答

0

看來你可能不瞭解「導入」的用法。

有訪問變量(或其他)的雙向: 1.

import getReady 

# you can getReady.salesPersonID 

2.

from getReady import salesPersonID 

#you can salesPersonID 

在點我個人的觀點,這些代碼是不是很Python的。^_^

+0

你可能沒錯。我在Python中添加了僞代碼,但混合程序似乎不能很好地工作。在這個階段,我真的想把更多的精力放在邏輯層面上。雖然我認爲一旦我更好地掌握python,我會很樂意與它合作。 – 2015-02-11 06:21:53