2017-02-23 7 views
0

元の質問は、第4章の終わりにボーリングを自動化し、第4章コンマコード - ブックから延長

[Pythonの3.xので書か]

コンマコード

は、あなたが持っていると言いますこのようなリスト値:

spam = ['apples', 'bananas', 'tofu', 'cats'] 

は、引数としてリスト値をとり、カンマで区切られたすべての項目に及びとして 文字列を返す関数を書きます最後の項目の前に'and' が挿入されています。たとえば、前の spamリストを関数に渡すと、'apples, bananas, tofu, and cats'が返されます。しかし、あなたの関数はそれに渡されたリストの値 で動作するはずです。

私の質問は次のとおりです。
私のようなプロンプトコマンドのリストに取るためにはPython 3.xので書かれたコードを変更したい:1, 2, 3, 4, 5とプログラムが1, 2, 3, 4 and 5を出しているが、私が達成しましたこの部分は私の(非常に長い)コードから見ることができます。問題は、数字と名前または単語のリストがあり、この結果が必要なときにコマンドプロンプトで入力します。1, 2, 3, 4, jeremy, annaと次の出力が必要です。1, 2, 3, 4, jeremy and anna 私のコードからわかるように、それ以外のアルファベットは解決策を見つける目的で簡単に説明しています。私がこの問題を解決しようとしたのは、検索などの正規表現関数を使用して出力にカンマを検索することでした。ロジックは、それらのカンマの位置を取得して、最後のカンマを'and'文字列に置き換えることでした。しかし、私はそれを達成するための何かを見つけることができませんでした。

p.s.私のコードは、(下)here

#! python3. 
#import re 
my_input = list(input('Please enter a list of words and or numbers separated by commas:\n')) 

def commacode(listname): 

#if the last entry is equal to a number between 0 and 9 put an "and" before it then print the modified input to screen  
    if listname[len(listname) - 1] == '0': 

     listname[len(listname) - 1] = 'and ' + listname[len(listname) -1] 
     index = 0 
     new_string = listname[index] 
     while index < len(listname)-1: 
      new_string = new_string + listname[index + 1] 

      index = index + 1 
      if index == len(listname)-1: 
       print('\n') 
       print(new_string) 

    elif listname[len(listname) - 1] == '1': 
     listname[len(listname) - 1] = 'and ' + listname[len(listname) -1] 
     index = 0 
     new_string = listname[index] 
     while index < len(listname)-1: 
      new_string = new_string + listname[index + 1] 

      index = index + 1 
      if index == len(listname)-1: 
       print('\n') 
       print(new_string) 

    elif listname[len(listname) - 1] == '2': 
     listname[len(listname) - 1] = 'and ' + listname[len(listname) -1] 
     index = 0 
     new_string = listname[index] 
     while index < len(listname)-1: 
      new_string = new_string + listname[index + 1] 

      index = index + 1 
      if index == len(listname)-1: 
       print('\n') 
       print(new_string) 

    elif listname[len(listname) - 1] == '3': 
     listname[len(listname) - 1] = 'and ' + listname[len(listname) -1] 
     index = 0 
     new_string = listname[index] 
     while index < len(listname)-1: 
      new_string = new_string + listname[index + 1] 

      index = index + 1 
      if index == len(listname)-1: 
       print('\n') 
       print(new_string)    

    elif listname[len(listname) - 1] == '4': 
     listname[len(listname) - 1] = 'and ' + listname[len(listname) -1] 
     index = 0 
     new_string = listname[index] 
     while index < len(listname)-1: 
      new_string = new_string + listname[index + 1] 

      index = index + 1 
      if index == len(listname)-1: 
       print('\n') 
       print(new_string)   

    elif listname[len(listname) - 1] == '5': 
     listname[len(listname) - 1] = 'and ' + listname[len(listname) -1] 
     index = 0 
     new_string = listname[index] 
     while index < len(listname)-1: 
      new_string = new_string + listname[index + 1] 

      index = index + 1 
      if index == len(listname)-1: 
       print('\n') 
       print(new_string) 

    elif listname[len(listname) - 1] == '6': 
     listname[len(listname) - 1] = 'and ' + listname[len(listname) -1] 
     index = 0 
     new_string = listname[index] 
     while index < len(listname)-1: 
      new_string = new_string + listname[index + 1] 

      index = index + 1 
      if index == len(listname)-1: 
       print('\n') 
       print(new_string) 

    elif listname[len(listname) - 1] == '7': 
     listname[len(listname) - 1] = 'and ' + listname[len(listname) -1] 
     index = 0 
     new_string = listname[index] 
     while index < len(listname)-1: 
      new_string = new_string + listname[index + 1] 

      index = index + 1 
      if index == len(listname)-1: 
       print('\n') 
       print(new_string) 

    elif listname[len(listname) - 1] == '8': 
     listname[len(listname) - 1] = 'and ' + listname[len(listname) -1] 
     index = 0 
     new_string = listname[index] 
     while index < len(listname)-1: 
      new_string = new_string + listname[index + 1] 

      index = index + 1 
      if index == len(listname)-1: 
       print('\n') 
       print(new_string) 

    elif listname[len(listname) - 1] == '9': 
     listname[len(listname) - 1] = 'and ' + listname[len(listname) -1] 
     index = 0 
     new_string = listname[index] 
     while index < len(listname)-1: 
      new_string = new_string + listname[index + 1] 

      index = index + 1 
      if index == len(listname)-1: 
       print('\n') 
       print(new_string) 

    elif listname[len(listname) - 1] == 'a': 
     listname[len(listname) - 1] = 'and ' + listname[len(listname) -1] #This is where I need the help in this elif statement.... 
     index = 0 
     new_string = listname[index] 
     while index < len(listname)-1: 
      new_string = new_string + listname[index + 1] 

      index = index + 1 
      if index == len(listname)-1: 
       print('\n') 
       print(new_string) 

    else: 
     print('\n') 
     print('Please enter lower case letters') 
     exit 

commacode(my_input) 

答えて

0

なぜそんなに長いを参照して、オリジナルのコンマコードの問題にスタックオーバーフロー上の誰かによって与えられた答えを使用していましたか?ここで は私です:ところで

spam = ['apple', 'bananas', 'tofu', 'cats'] 

def func(a): 
    a[-1] = 'and '+ a[-1] 
    print(a) 

func(spam) 

、私はこの本を読んでいますが、私はまだなどのものに到達しなかった、私はあなたの質問に答えるのですか?

0

ご協力いただきありがとうございます。それは私の質問にはあまり答えません。別のフォーラムからこのコードは、しかし、私の質問に答えるん:私が管理できるよりもはるかに少ないラインで

とはいを。

関連する問題