2014-04-01 13 views
0

我有一個簡單的Python程序,我正在開發,而且我是編程新手,並且工作不太好。我仍然在學習模塊和函數,但這裏是程序要求。我無法理解程序的導入和調用部分以及模塊參數參數,以及全局變量和局部變量。我仍然在學習,所以任何反饋或建議都會有所幫助。我的書似乎主要是用僞代碼編寫的,而不是真正的Python代碼,它讓事情變得混亂。用於簡單指數計算的Python模塊

''' 
Write a program that has thee modules: 

1) the first module takes two numbers from the user 

2) the second module calculate firstNumber to the power of secondNumber and prints it 

3) the third module will print 'end of program' 
''' 

Module main() 
    #Global Variables 
    Declare Integer num1 
    Declare Integer num2 
    #Welcome Exponent Power Program 
    Print ('Welcome to the Exponent Powers Program.') 
    Call askNumbers(num1, num2) 
    Call numberPower 
    Call endProgram 
End Module 

#This program gets the 2 numbers from the user 
Module askNumbers(Integer num1, Integer num2) 
    num1 = int(raw_input('Enter Number : ')) 
    num2 = int(raw_input('Enter Raised Power Number: ')) 
End Module 

Module numberPower(int num1, int num2) 
    Declare Int expTotal 
    Set expTotal = (num1**num2) 
    Print(expTotal) 
End Module 

Module endProgram() 
    Print('End of Program') 
End Module 

偉大的幫助,並再次感謝您的時間。

+0

這是VisualBasic中...爲什麼你在Python過程中使用VBA的書?這很奇怪 –

回答

0

每個模塊將是一個單獨的.py文件。 當你調用import <module name>它會通過自上而下的模塊。如果我有一個模塊運行,增加的功能/參數是目前所有的定義,並運行任何代碼,如果是第一次進口

foo。PY

print "foo" 

當我輸入foo的,有要執行的動作,所以你會看到

空閒

>>> import foo 
foo 
>>> 

如果我把一個函數,什麼都不會打印,但是我將能夠使用foo模塊名下的功能

foo.py

def foo(): 
    print "foo" 

空閒

>>> import foo 
>>> foo.foo() 
foo 
# or using the from keyword I can get the function as just foo 
>>> from foo import foo 
>>> foo() 
foo 

如果我想從另一個模塊調用foo說bar.py,我會做這樣的事情

bar.py

from foo import foo 
foo() 
print "bar" 

空閒

>>>import bar 
foo 
bar 
>>> 

所以你的情況:

module1.py

a = int(raw_input("enter number 1: ")) 
b = int(raw_input("enter number 2: ")) 

module2.py

from module1 import a, b 
print a**b 

module3.py

import module2 
print 'end of program' 

空閒

>>> import module3 
enter number 1: 2 
enter number 2: 3 
8 
end of program 
>>> 
0

替換所有Moduledef

刪除所有End Module

添加冒號(:)到每個def行的末尾

刪除所有Set

變化Printprint

刪除intInteger

任何地方它說:call some_module的話與some_module()

替換刪除任何Declare

,我想,你有蟒蛇

如:

Module endProgram() 
    Print('End of Program') 
End Module 

成爲

def endProgram(): 
    print "End of Program" 

它使用僞代碼(和進出口甚至不能肯定這就是僞代碼看起來像帕斯卡或東西(也許vbasic))的原因是因爲他們正試圖教給你的概念不只是如何複製和粘貼

快速谷歌搜索變成這實際上是Visual Basic

0
#This askNumber Module gets the 2 numbers from the user, and calls these 2 
def askNumbers(): 
    print 'Welcome to Python Exponent Program' 
    num1 = int(raw_input('Enter Number 1: ')) 
    num2 = int(raw_input('Enter Raised to Power Number 2: ')) 
    numberPower(num1, num2) 

#This numberPower is the exponent calculation module 
def numberPower(num1, num2): 
    expTotal = (num1**num2) 
    print('Number 1 to the power of Number 2 is: ', expTotal) 

#This module prints end Program  
def endProgram(): 
    print 'End of Program' 

askNumbers() 
endProgram() 

#test 5^5: 3125 
#test 3^4: 81 
#test 7^6: 117649 
#test 2^2: 4