正如标题所述,我在将函数导入main.py文件时遇到问题。我将我的项目(有史以来第一个个人项目)保存到名为MINGW32:c/DEV/Shayne/Quarks
的文件夹中
在该文件夹中,我有以下文件
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
从shell运行 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
答案 0 :(得分:0)
确保从可以访问commands.py文件的上下文中执行脚本。如果您从命令行执行它应该没有问题(可能在奇怪的权限问题之外),但是如果您使用IDE,请确保PYTHONPATH包含您的文件的位置。
答案 1 :(得分:0)
经过一些基本的调试(你在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.'