SORU
25 NİSAN 2014, Cuma


Geçerli bir yanıt verene kadar, giriş için Kullanıcı soran

Kullanıcı girişi kabul etmek için bir program yazıyorum.

#note: Python 2.7 users should use `raw_input`, the equivalent of 3.X's `input`
age = int(input("Please enter your age: "))
if age >= 18: 
    print("You are able to vote in the United States!")
else:
    print("You are not able to vote in the United States.")

Bu kullanıcının hassas veri girerse beklendiği gibi çalışır.

C:\Python\Projects> canyouvote.py
Please enter your age: 23
You are able to vote in the United States!

Ama eğer bir hata yaparsanız, o zaman çöküyor

C:\Python\Projects> canyouvote.py
Please enter your age: dickety six
Traceback (most recent call last):
  File "canyouvote.py", line 1, in <module>
    age = int(input("Please enter your age: "))
ValueError: invalid literal for int() with base 10: 'dickety six'

Çökmesini yerine, tekrar giriş yapmaya çalış etmek istiyorum. Bu gibi:

C:\Python\Projects> canyouvote.py
Please enter your age: dickety six
Sorry, I didn't understand that.
Please enter your age: 26
You are able to vote in the United States!

Bunu nasıl yapabilirim? Eğer ben de int ama bu anlamsız geçerli bir bağlam olan -1 gibi değerleri reddetmek istediyse?

CEVAP
25 NİSAN 2014, Cuma


Bunu yapmanın en kolay yolu bir süre içinde döngü input yöntemi koymak olacaktır. Kötü giriş ve tatmin olduğunuzda break döngü çıkınca continue kullanın.

Giriş özel bir Durum ortaya Çıkabilir

Kullanıcı ayrıştırılabilir mi veri girdiğinde try and catch algılamak için kullanın.

while True:
    try:
        # Note: Python 2.x users should use raw_input, the equivalent of 3.x's input
        age = int(input("Please enter your age: "))
    except ValueError:
        print("Sorry, I didn't understand that.")
        #better try again... Return to the start of the loop
        continue
    else:
        #age was successfully parsed!
        #we're ready to exit the loop.
        break
if age >= 18: 
    print("You are able to vote in the United States!")
else:
    print("You are not able to vote in the United States.")

Kendi Doğrulama Kuralları Uygulamak

Eğer Python başarılı bir şekilde ayrıştırabilir değerleri reddetmek istiyorsanız, kendi doğrulama mantığını ekleyebilirsiniz.

while True:
    data = input("Please enter a loud message (must be all caps): ")
    if not data.isupper():
        print("Sorry, your response was not loud enough.")
        continue
    else:
        #we're happy with the value given.
        #we're ready to exit the loop.
        break

while True:
    data = input("Pick an answer from A to D:")
    if data.lower() not in ('a', 'b', 'c', 'd'):
        print("Not an appropriate choice.")
    else:
        break

Özel Durum İşleme ve Özel Doğrulama birleştirerek

Yukarıdaki teknikleri hem de bir döngü içine kombine edilebilir.

while True:
    try:
        age = int(input("Please enter your age: "))
    except ValueError:
        print("Sorry, I didn't understand that.")
        continue

    if age < 0:
        print("Sorry, your response must not be negative.")
        continue
    else:
        #age was successfully parsed, and we're happy with its value.
        #we're ready to exit the loop.
        break
if age >= 18: 
    print("You are able to vote in the United States!")
else:
    print("You are not able to vote in the United States.")

Bunu bir İşlev olarak şifrelenmiş

Eğer farklı değerler bir sürü için Kullanıcı sormak gerekirse, bir işlevi bu kodu koymak için yararlı olabilir, her zaman yeniden yazmak zorunda kalmazsınız.

def get_non_negative_int(prompt):
    while True:
        try:
            value = int(input(prompt))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue

        if value < 0:
            print("Sorry, your response must not be negative.")
            continue
        else:
            break
    return value

age = get_non_negative_int("Please enter your age: ")
kids = get_non_negative_int("Please enter the number of children you have: ")
salary = get_non_negative_int("Please enter your yearly earnings, in dollars: ")

Hepsini bir araya getirirsek

Bu fikir çok genel bir giriş fonksiyonu yapmak için uzatabilirsiniz:

def sanitised_input(prompt, type_=None, min_=None, max_=None, range_=None): 
    if min_ is not None and max_ is not None and max_ < min_: 
        raise ValueError("min_ must be less than or equal to max_.") 
    while True: 
        ui = input(prompt) 
        if type_ is not None: 
            try: 
                ui = type_(ui) 
            except ValueError: 
                print("Input type must be {0}.".format(type_.__name__)) 
                continue
        if max_ is not None and ui > max_: 
            print("Input must be less than or equal to {0}.".format(max_)) 
        elif min_ is not None and ui < min_: 
            print("Input must be greater than or equal to {0}.".format(min_)) 
        elif range_ is not None and ui not in range_: 
            if isinstance(range_, range): 
                template = "Input must be between {0.start} and {0.stop}."
                print(template.format(range_)) 
            else: 
                template = "Input must be {0}."
                if len(range_) == 1: 
                    print(template.format(*range_)) 
                else: 
                    print(template.format(" or ".join((", ".join(map(str, 
                                                                     range_[:-1])), 
                                                       str(range_[-1]))))) 
        else: 
            return ui 

Kullanımı gibi:

age = sanitised_input("Enter your age: ", int, 1, 101)
answer = sanitised_input("Enter your answer", str.lower, range_=('a', 'b', 'c', 'd'))

Neden Onlardan sakınmanız Gerektiğini ortak Tuzaklar, ve

input Gereksiz ifadelerin Gereksiz Kullanımı

Bu yöntem çalışır ama genellikle stil fakir olarak kabul edilir:

data = input("Please enter a loud message (must be all caps): ")
while not data.isupper():
    print("Sorry, your response was not loud enough.")
    data = input("Please enter a loud message (must be all caps): ")

Başlangıçta while True yöntem daha kısa çünkü çekici görünebilir, ama yazılım geliştirme Don't Repeat Yourself ilkesini ihlal ediyor. Bu sistem hataları olasılığını artırır. Eğer input 31 ** ama yanlışlıkla 32 ** sadece ilk olarak değiştirin değiştirerek 2.7 dahil olmak istiyorsanız ne? SyntaxError bir gerçekleşmeyi bekleyen.

Özyineleme Yığını Esecek

Eğer sadece özyineleme hakkında öğrendim, döngü sırasında imha edebilirsiniz get_non_negative_int kullanmak için cazip olabilir.

def get_non_negative_int(prompt):
    try:
        value = int(input(prompt))
    except ValueError:
        print("Sorry, I didn't understand that.")
        return get_non_negative_int(prompt)

    if value < 0:
        print("Sorry, your response must not be negative.")
        return get_non_negative_int(prompt)
    else:
        return value

Bu sorun çoğu zaman iş gibi görünüyor, ama eğer kullanıcı geçersiz veri yeterli bir kez girerse, script RuntimeError: maximum recursion depth exceeded ile sonlandırılacak. Düşünebilirsiniz "aptal bir satırda 1000 hatalar yapar", ama aptallar bir marifet küçümsüyorsun!

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Canal de arkyoru

    Canal de ark

    18 Ocak 2007
  • ELPRESADOR

    ELPRESADOR

    21 HAZİRAN 2008
  • xiaoyu85

    xiaoyu85

    20 ŞUBAT 2010