2016-12-17 5 views
-1

私はまだPythonでクラスに慣れようとしています。私は休暇中ですので、インターネットはありませんので、チュートリアルを参照することはできません。したがって、私は彼らに自分自身を教えようとしています。私はこれに関連する質問をずっと前に尋ねて、その後、コードをほぼ完全に変更し、コードの大部分を短縮しましたが、まだエラーがあるようです。私は考えることができるすべてを試しましたが、それは私が行方不明になっている可能性が最も単純です。以下のコードと出力:クラスでの問題

プログラムは、人々が飛行機上でどのタイプの座席を望んでいるかを判断しようとしています。

クラス:

import type_seat 
# Choose the seat to book 
print("=" * 170) 
print("Welcome to Etihad! This program can help you organize your flight, " 
     "payments and usage of miles!") 
possible_types = [] 
possible_types.extend(["Low_Economy", "Standard_Economy", "High_Economy", 
         "Business", "First", "Residence"]) 
seat_type = input("What type of ticket would you like? The possible types " 
        "are: {}. ".format(possible_types)) 
type_seat.SeatBooking(seat_type) 

print("You have chosen to book a {} ticket.".format(seat_type)) 
confirmation = input("Please confirm with 'Yes' or 'No': ").lower() 
if confirmation == "yes": 
    print("Excellent decision! Ready to continue") 
    print("=" * 170) 
elif confirmation == "no": 
    seat_type = str(input("What type of ticket would you like? The " 
          "possible types are: {} ".format(possible_types))) 
    type_seat.SeatBooking(seat_type) 
else: 
    print("That doesn't seem to be a valid answer.") 

出力(I入力はイタリック太字で示されているもの):

class SeatBooking: 
    def __init__(self, seat): 
     self.seat = seat 
     possible_types = ["Low_Economy", "Standard_Economy", "High_Economy", 
          "Business", "First", "Residence"] 
     while True: 
      if self.seat.lower() not in possible_types: 
       print("Sorry, but this is not a valid answer. " 
         "Please try again!") 
       break 
      else: 
       continue 

メインコード(用語が何であれ 'コール' クラス、またはに)

  • エティハドへようこそ!このプログラムは、あなたのフライト、支払い、マイルの使用を整理するのに役立ちます!
  • どのような種類の航空券を希望しますか? 「Low_Economy」、「Standard_Economy」、「High_Economy」、「Business」、「First」、「Residence」のようなタイプが考えられます。 レジデンス
  • 申し訳ありませんが、これは有効な回答ではありません。もう一度お試しください!
  • 宿泊券を予約することを選択しました。
  • は、 'はい' または 'いいえ' でご確認ください:はい
  • 優れた意思決定を!

    1. は、なぜそれがまだ出力が「申し訳ありませんが、これは有効な回答ではありません」ん:

私の質問を継続する準備はできましたか?

  • また、私は何かを入力できるようです。
  • このコードの「古い」バージョンを確認してください。なぜそれが動作しないのかがわかります。しかし、それにも問題がありました。 :(

    リンク:Issues with lists? Error checking not working

    +0

    をあなたが本当に知りたい、と私はあなたのためにそれを編集した場合、それは非常に遅いですし、私は私がビデオを見ることができない意味チュートリアル。私は書かれたものからも学ばず、クラスに関して私を混乱させました。それは要点以外にもあります。 – Eric

    答えて

    0

    この条件:

    possible_types.extend(["Low_Economy", "Standard_Economy",  "High_Economy", "Business", "First", "Residence"]) 
    

    大文字を持っている、これは衝突します:

    if self.seat.lower() not in possible_types: 
    

    あなたは、座席が小文字には可能なタイプを変換します。

    あまり関係の問題:作成するとき

    リスト内のデータを直接挿入します。

    possible_types = ["Low_Economy","Standard_Economy","High_Economy","Business", "First", "Residence"] 
    

    を私はあなたが拡張を使用する必要がある理由表示されていない、と私はあなたが宣言し、なぜ表示されませんそれは二度。

    ユーザーが入力できるようにすると、同様のものを入力できます。 LHS上の値はすべて小文字で、右辺の値は大文字と小文字が混在しているので、

    0

    この

    possible_types = ["Low_Economy", "Standard_Economy", "High_Economy", 
             "Business", "First", "Residence"] 
        while True: 
         if self.seat.lower() not in possible_types: 
    

    は、仕事に行くことはありません。おそらくpossible_typesのエントリを小文字にするのが最も簡単です...

    0

    あなたの例ではいくつかの問題があります。

    • 入力は小文字に変換されますが、リストには大文字と小文字の両方の文字列が含まれています。
    • がループを壊さないので、リストがinの場合、あなたは無期限にループします!

      • possible_typesがクラスにし、あなたのコード内で定義されています

      また、他のデータ型を使用して解決することができ、いくつかのマイナーな非効率性があります。両方に1つの変数を使用できます。

    • ルックアップlistの平均はO(n)です。set(またはfrozensetが変更されていない場合)を使用して、O(1)ルックアップ時間を持つことができます。要するに

    次のように私はそれを書き直します:

    # List of possible types: 
    possible_types = ["Low_Economy", "Standard_Economy", "High_Economy", "Business", "First", "Residence"] 
    
    class SeatBooking: 
        # a set of possible types: 
        possible_types = frozenset(seat_type.lower() for seat_type in possible_types) 
    
        def __init__(self, seat): 
         # Just check if it's in the possible types otherwise raise an Exception. 
         if seat.lower() not in self.possible_types: 
          raise ValueError("Sorry, but this is not a valid answer.") 
         self.seat = seat 
    
    # Use the `while` loop where you expect the `input`: 
    while True: 
        seat_type = input("What type of ticket would you like? The possible types " 
             "are: {}. ".format(possible_types)) 
        try: 
         # Try to create an instance and if it works break out of the loop 
         seat = SeatBooking(seat_type) 
         break 
        except ValueError as exc: 
         # Print the string saved in the exception. 
         print(exc)