bigmoumou 发表于 2015-10-30 19:05:04

python 作業 18 問題@@''

本帖最后由 bigmoumou 于 2015-10-30 19:21 编辑

# 大家好我是python入門新手:big
# 今天剛做到18回,但在 input時出了問題
# 我是使用 py35
# input 完x後,y就不讓我 input了,想請教大大們要如何修正?:cry
# 我的代碼如下 :
#------------------------------------------
# 題目是要在下方這個字串,搜索出'im'出現次數。
'''You cannot improve your past, but you can improve your future.
Once time is wasted, life is wasted.'''
#------------------------------------------

def findstr(x,y):
    count = 0
    if y not in x:
      print('找尋不到目標。')
    else:
      for i in range(len(x)-1):
            if x == y:
                if x == y:
                  count += 1
      
    print('總共有 %d 個物件' % count)

x = input('請輸入目標字串:')
y = input('請輸入搜索項目:')

杰里·桑德斯 发表于 2015-10-30 19:50:51

本帖最后由 杰里·桑德斯 于 2015-10-30 20:04 编辑

怎么没有看到你函数的调用? findstr(x, y)?

简单的方法就是使用
x = 'blablabla'
y = 'im'
x.count(y)
这样来搜寻字串里的物件。

如果非要用以上函数的话...請看下面...

bigmoumou 发表于 2015-10-30 20:02:43

我知道問題了... 原來 You cannot improve your past, but you can improve your future.
Once time is wasted, life is wasted. 多行註解多了個' ',貼進去反而跳錯... 直接複製就沒問題

杰里·桑德斯 发表于 2015-10-30 20:03:13

好了,搞定了。

def findstr(x, y):
        count = 0
        if y not in x:
                print('找尋不到目標嘛')
        else:
                for i in range(len(x)-1):
                        if x == y:
                                if x == y:
                                        count += 1
                                else:
                                        pass
                        else:
                                pass
               
                print('總共有 %d 個物件' % count) #這個地方你少縮進了一次,print不出來
                               
x = '''You cannot improve your past, but you can improve your future.
Once time is wasted, life is wasted.'''
y = input('請輸入目標字串: ')

findstr(x, y)

這個你試試看能否運行。

小甲鱼的二师兄 发表于 2015-10-31 21:11:27

Python问题建议在Python交流版块提问哦
页: [1]
查看完整版本: python 作業 18 問題@@''