2017-08-02 11 views
0
#room B register 
#matrix method 
roomB = [[],[]]  

私はここにroomB = [[]、[]]の3つのユニットしか入力しないで、最初のユニットがいっぱいであれば、システムは別のユニットを提案する必要があります。pythonでリストの入力制限を設定する方法3

def func(): 
    row = int(input("Choose 0 or 1:")) 
    if (row == 0):     # ROW 0 IS FOR HOUSE 1: 
      name = input("Enter your room name: ") 
      print("Enter M or N")  #M for master room 
      room_type = input("")   #N for normal room 
      for u in roomB:  #3 units in roomB[0] 
       if(len(u)<3): 
       if (room_type == "M"): 
        return roomB[0].append([room_type,name,140]) 
       if (room_type == "N"): 
        return roomB[0].append([room_type,name,140]) 
+0

あなたの問題は何ですか?エラーや予期しない動作が発生しますか? –

+0

あなたはどんな出力を期待していますか? – Gahan

+0

エラーは発生しませんでしたが、私の出力に私のリストに3ユニットのみを挿入したいのですが、私はchgするべきですか? – Jordan

答えて

0
roomB = [[],[]] 

def update(row): # takes sublist as argument 
    if len(roomB[row]) < 3: # checks if sublist length is less than 3 
     name = input("Enter your name: ") 
     room_type = input("Enter room type : M (master room) or N (normal room) : ").lower() 
     roomB[row].append([room_type,name,140]) 
     return "your room no. is {} at row {}".format(roomB[row].index([room_type,name,140]) + 1, row) # returns string stating status 


def func(): 
    if len(roomB[0]) < 3 and len(roomB[1]) < 3: # ask for prompt only if space available in both sublist 
     row = int(input("Choose 0 or 1: ")) # allow to select row 
     return update(row) 
    elif len(roomB[0]) >= 3 and len(roomB[1]) < 3: # selects default sublist if no space on other sublist 
     print("No room available at 0 , selected 1 ") 
     return update(1) # returns string stating status 
    elif len(roomB[0]) < 3 and len(roomB[1]) >= 3: # selects default sublist if no space on other sublist 
     print("No room available at 1 , selected 0 ") 
     return update(0) 
    else: # in case any error occurs it goes here 
     print("Errrr.......") 
     print("room stat....: ", roomB) 

while len(roomB[0]) <= 3 or len(roomB[1]) <= 3: # loop will flow if length of both sublist is greater than or equals to 3 
    if len(roomB[0]) != 0 or len(roomB[1]) != 0: # won't ask for confirmation if all all lists are empty (skips prompts for initial) 
     cond = input("do you want to continue?(Y/N) ").lower() 
     if cond == "n": 
      break # break the loop if pressed n or N otherwise continue to execute 
    elif len(roomB[0]) >= 3 and len(roomB[1]) >= 3: # error message to print if no space left in sublists 
     print("No room available.. Sorry for inconvinience.") 
    print(func()) 
+0

あなたのソリューションに感謝btwなぜ私たちは追加の代わりにフォーマットを使用する必要がありますか?そして、私は1つだけのマスターベッドルームと2つの通常のベッドルームを持っている部屋が欲しい – Jordan

+0

私は追加を使用しています。 appendは要素をリストに追加するだけなので、 'return list.append()'と書くとNone(関数)はNoneを返します。 formatはPythonの文字列フォーマットです。私がすでに書いたように、文字列 – Gahan

+0

を返すことができます。もしそれが役に立ったら、回答として受け入れて受け入れることができます。 – Gahan

関連する問題