如标题所示,我在将函数导入到main.py文件时遇到了麻烦。我将我的项目(有史以来第一个个人项目)保存到名为的文件夹中BillApp
。
在该文件夹中,我有以下文件。
main.py
commands.py
billapp.txt
我正在尝试导入commands.py
我的main.py
代码,以便可以使用中的功能main.py
。
# This is the main code which will access the commands from commands.py
import commands
print 'Hello, Welcome to your friendly text-based bill reminder.'
print '\nI will be tracking all of your bills so you will never miss a payment again.'
print '\nLet\'s get started, please type on of the following commands.'
def userSelection():
userSelection = raw_input('New \nSaved \nRemove \nAlerts')
userSel = userSelection.lower()
if userSel == 'new':
commands.getDesc()
elif userSel == 'saved':
commands.savedBills()
elif userSel == 'remove':
commands.removeBills()
elif userSel == 'alerts':
commands.setAlerts()
else:
print 'Please enter a valid command'
userSelection()
这是我的commands.py
文件,以防万一你想看看。
def userSelection():
userSelection = raw_input('New \nSaved \nRemove \nAlerts')
userSel = userSelection.lower()
if userSel == 'new':
getDesc()
elif userSel == 'saved':
savedBills()
elif userSel == 'remove':
removeBills()
elif userSel == 'alerts':
setAlerts()
else:
print 'Please enter a valid command'
userSelection()
def getDesc():
desc = raw_input('\nHow do you want to describe this bill?\n')
def getAmnt():
try:
billAmnt = raw_input('How much is this bill?')
amnt = float(billAmnt.strip('$'))
return amnt
except ValueError:
print 'Please enter an amount that is at least 0'
getAmnt()
def getDate():
try:
strDate = raw_input('What date of the month is this bill due?\nExample: 3rd\n')
date = strDate.strip('abcdefghijklmnopqrstuvqxyz')
numDate = int(date)
print numDate
except ValueError:
print 'Please enter the day of the month, for example. 3rd or 17th.'
def getFreqy():
strFreqy = raw_input('How often do you have to pay this bill? \nMonthly, Weekly, or Once\n')
freqy = strFreqy.lower()
if freqy == 'monthly':
print 'monthly'
elif freqy == 'weekly':
print 'weekly'
elif freqy == 'once':
print 'once'
else:
print 'Please enter either monthly, weekly, or once.'
getFreqy()
def savedBills():
print 'Here is a list of all of your bills.'
# retrieve bills from database
def removeBills():
print 'You wish to remove a bill from your list, very well, let\'s get started.',
print 'Here\'s a list of all of your bills.'
# retrieve a numbered list of all bills
billNum = raw_input('What number bill would you like to remove?')
if int.billNum:
print 'We will remove that bill from your list.'
waiting()
else:
print 'Please type in a corresponding number.'
def setAlerts():
# pulls the current bill alert from database
print 'Your bill alerts are currently set to %s.' % currentAlert
change = raw_input("Would you like to change this?")
if change == yes:
# change bill alert in database
else change == no:
print 'Your bill alerts will continue as scheduled'
elif:
print 'Please enter either yes or no.'
main.py
从外壳运行,我得到以下信息。
MacBook-Pro:BillApp cmaher92$ sudo python main.py
Hello, Welcome to your friendly text-based bill reminder.
I will be tracking all of your bills so you will never miss a payment again.
Let's get started, please type on of the following commands.
New
Saved
Remove
Alerts
>new
Traceback (most recent call last):
File "main.py", line 22, in <module>
userSelection()
File "main.py", line 11, in userSelection
commands.getDesc()
AttributeError: 'module' object has no attribute 'getDesc'
Connor-Mahers-MacBook-Pro:BillApp cmaher92$
一些基本的调试后(你混了else
和elif
一些缩进和其他错误等中change == yes
),这似乎为我工作。从commands.py
这里导入不是问题。
至少我到达了它要求的部分
您想如何描述这笔帐单?
main.py
# This is the main code which will access the commands from commands.py
import commands
def userSelection():
userSelection = raw_input('New \nSaved \nRemove \nAlerts')
userSel = userSelection.lower()
if userSel == 'new':
commands.getDesc()
elif userSel == 'saved':
commands.savedBills()
elif userSel == 'remove':
commands.removeBills()
elif userSel == 'alerts':
commands.setAlerts()
else:
print 'Please enter a valid command'
userSelection()
print 'Hello, Welcome to your friendly text-based bill reminder.'
print '\nI will be tracking all of your bills so you will never miss a payment again.'
print '\nLet\'s get started, please type on of the following commands.'
userSelection()
commands.py
def userSelection():
userSelection = raw_input('New \nSaved \nRemove \nAlerts')
userSel = userSelection.lower()
if userSel == 'new':
getDesc()
elif userSel == 'saved':
savedBills()
elif userSel == 'remove':
removeBills()
elif userSel == 'alerts':
setAlerts()
else:
print 'Please enter a valid command'
userSelection()
def getDesc():
desc = raw_input('\nHow do you want to describe this bill?\n')
def getAmnt():
try:
billAmnt = raw_input('How much is this bill?')
amnt = float(billAmnt.strip('$'))
return amnt
except ValueError:
print 'Please enter an amount that is at least 0'
getAmnt()
def getDate():
try:
strDate = raw_input('What date of the month is this bill due?\nExample: 3rd\n')
date = strDate.strip('abcdefghijklmnopqrstuvqxyz')
numDate = int(date)
print numDate
except ValueError:
print 'Please enter the day of the month, for example. 3rd or 17th.'
def getFreqy():
strFreqy = raw_input('How often do you have to pay this bill? \nMonthly, Weekly, or Once\n')
freqy = strFreqy.lower()
if freqy == 'monthly':
print 'monthly'
elif freqy == 'weekly':
print 'weekly'
elif freqy == 'once':
print 'once'
else:
print 'Please enter either monthly, weekly, or once.'
getFreqy()
def savedBills():
print 'Here is a list of all of your bills.'
# retrieve bills from database
def removeBills():
print 'You wish to remove a bill from your list, very well, let\'s get started.',
print 'Here\'s a list of all of your bills.'
# retrieve a numbered list of all bills
billNum = raw_input('What number bill would you like to remove?')
if int.billNum:
print 'We will remove that bill from your list.'
waiting()
else:
print 'Please type in a corresponding number.'
def setAlerts():
# pulls the current bill alert from database
print 'Your bill alerts are currently set to %s.' % currentAlert
change = raw_input("Would you like to change this?")
if change == 'yes':
# change bill alert in database
pass
elif change == 'no':
print 'Your bill alerts will continue as scheduled'
else:
print 'Please enter either yes or no.'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句