[英]How to call functions in python
我试图在我的程序中使用函数,但我不完全确定如何使用它们,我一直在使用 python 一段时间,但从未使用过函数,谁能解释为什么这对我不起作用?
import csv
import operator
def SerachForCapitals():
#opens and reads txt file
sample = open('Capitals.txt', 'r')
csv1 = csv.reader(sample, delimiter = ',')
#asks user for an input
name = raw_input('enter a capital: ')
#sets the headings
print ""
print "Capital, Country"
print ""
#Finds capital row and prints it
for row in csv1:
if name == row[0]:
print row
print ' '
def AddInData():
#opens the text file
sample = open('capitals.txt','a+')
csv1 = csv.reader(sample, delimiter = ',')
#prints headings
print 'capital, country'
#ask ser for an input
change = raw_input('add data like line shown above:')
#adds input to text file
sample.write(change)
#closes the text file
sample.close()
#Menu choices
print '1. choose a capital'
print '2. enter data'
#Get an input from the user
menuC= raw_input('please choose a menu item: ')
if menuC == '1':
SearchForCapitals()
elif menuC == '2':
AddInData()
除此之外,您对SearchForCapitals
的函数定义有一个错字,我的意思是该行
def SerachForCapitals():
应该
def SearchForCapitals():
(注意Search
的拼写),你的代码对我来说非常好。
如果这不能解决您的问题,您应该提供运行代码时获得的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.