我不明白如何保存输入字符串,然后在init()中保存它以便以后在另一个函数中使用
Class Person:
def __init__ (self):
....
def cCount (self):
num_A = self.count('A')
return num_A
import sys
def main():
inX = input('sequence?')
while inX
myY = Person(inX)
myCnumber = myY.cCount()
print (" {0}".format(myCnumber))
...
我希望输出为用户输入字符串中As的数量的计数
答案 0 :(得分:0)
您可以像这样重新排列代码,以实现所需的目标:
class Person:
def __init__ (self, sequence):
self.sequence = sequence
def cCount (self):
return self.sequence.count('A')
import sys
def main():
inX = input('sequence?')
while inX:
myY = Person(inX)
myCnumber = myY.cCount()
print (" {0}".format(myCnumber))
inX = input('sequence?')
main()
结果
sequence?ABCDEF
1
sequence?XAAAEWF
3
sequence?
所做的更改
self.sequence
并对其进行了初始化def cCount
中,更改为self.sequence.count('A')并返回