Hướng dẫn how to call a function again and again in python - cách gọi đi gọi lại một hàm trong python

Tôi khá mới đối với Python, tôi chỉ tự hỏi cách tốt nhất để làm điều này [câu hỏi rất đơn giản]:

Tôi có một chức năng nhắc người dùng chọn tùy chọn, nếu tùy chọn họ nhập không hợp lệ, nó sẽ gọi lại chức năng:

def choose_colour[]:
    print "Select colour:"
    print "1. Red"
    print "2. Blue"
    print "3. Green"
    selection = raw_input[]

Ban đầu tôi đã có một câu lệnh IF so sánh đầu vào và nếu đó là một đầu vào không chính xác, tôi sẽ gọi lại chức năng trong chính nó. Mà không thực tế và sai.

def choose_colour[]:
    print "Select colour:"
    print "1. Red"
    print "2. Blue"
    print "3. Green"
    selection = raw_input[]

    if selection == "1":
         colour = "Red"
    elif selection == "2":
         colour = "Blue"
    elif selection == "3":
         colour = "Green"
    else:
         print "Please select, 1, 2 or 3"
         choose_colour[]

Chỉnh sửa:

Điều này hoạt động nhưng tôi thích giải pháp vòng lặp.

def set_TPL_colour[]:
    print "Enter TPL colour value:"
    print "1. WHITE"
    print "2. GREEN"
    print "3. AMBER"
    print "4. RED"
    print ">>>",
    option = raw_input[]
    if option == "1":
        tpl_colour = "WHITE"
        return tpl_colour
    elif option == "2":
        tpl_colour = "GREEN"
        return tpl_colour
    elif option == "3":
        tpl_colour = "AMBER"
        return tpl_colour
    elif option == "4":
        tpl_colour = "RED"
        return tpl_colour
    else:
        print "ERROR incorrect option selection"
        tpl_colour = set_TPL_colour[]
        return tpl_colour

hỏi ngày 31 tháng 7 năm 2015 lúc 3:48Jul 31, 2015 at 3:48

LizzeiylizzeiyLizzeiy

Huy hiệu vàng 4081 Huy hiệu bạc22 Huy hiệu đồng1 gold badge8 silver badges22 bronze badges

2

Sử dụng một từ điển, ví dụ -

def choose_colour[]:
    print "Select colour:"
    print "1. Red"
    print "2. Blue"
    print "3. Green"
    colordict = {"1":"Red" , "2":"Blue", "3":"Green"}
    selection = raw_input[]
    ret = colordict.get[selection]
    if not ret:
        return choose_colour[]
    return ret

Bạn cũng có thể làm trong khi vòng lặp -

def choose_colour[]:
    colordict = {"1":"Red" , "2":"Blue", "3":"Green"}
    ret = None
    while not ret:
        print "Select colour:"
        print "1. Red"
        print "2. Blue"
        print "3. Green"
        selection = raw_input[]
        ret = colordict.get[selection]
    return ret

Ví dụ/Demo -

>>> def choose_colour[]:
...     print "Select colour:"
...     print "1. Red"
...     print "2. Blue"
...     print "3. Green"
...     colordict = {"1":"Red" , "2":"Blue", "3":"Green"}
...     selection = raw_input[]
...     ret = colordict.get[selection]
...     if not ret:
...         return choose_colour[]
...     return ret
...
>>> choose_colour[]
Select colour:
1. Red
2. Blue
3. Green
5
Select colour:
1. Red
2. Blue
3. Green
2
'Blue'

Đã trả lời ngày 31 tháng 7 năm 2015 lúc 4:01Jul 31, 2015 at 4:01

Anand s Kumaranand s KumarAnand S Kumar

85,7K18 Huy hiệu vàng181 Huy hiệu bạc171 Huy hiệu đồng18 gold badges181 silver badges171 bronze badges

Bạn không cần gọi lại chức năng, chỉ cần đặt nó vào vòng lặp trong một thời gian và gán lại biến nếu đầu vào không hợp lệ:

def choose_colour[]:
    print "Select colour:"
    print "1. Red"
    print "2. Blue"
    print "3. Green"
    selection = raw_input[]
    while selection not in ["1","2","3"]:
        print "Please choose a valid option"
        selection = raw_input[]
    if selection == "1":
        colour = "Red"
    elif selection == "2":
        colour = "Blue"
    elif selection == "3":
        colour = "Green"

Phiên bản sạch hơn:

def choose_colour[]:
    colourChoices = {"1":"Red", "2":"Blue", "3":"Green"}
    selection = raw_input["Select colour:\n 1. Red\n 2. Blue\n 3. Green"]
    while !colourChoices[selection]:
        print "Please enter valid data"
        selection = raw_input["Select colour:\n 1. Red\n 2. Blue\n 3. Green"]
    colour = colourChoices[selection]

Đã trả lời ngày 31 tháng 7 năm 2015 lúc 3:54Jul 31, 2015 at 3:54

user3636636user3636636user3636636

2.31915 huy hiệu bạc31 huy hiệu đồng15 silver badges31 bronze badges

1

Một cái gì đó như thế này.

________số 8

Đã trả lời ngày 31 tháng 7 năm 2015 lúc 3:53Jul 31, 2015 at 3:53

Pushkinpushkinpushkin

8.97715 Huy hiệu vàng52 Huy hiệu bạc92 Huy hiệu Đồng15 gold badges52 silver badges92 bronze badges

Bạn có thể sử dụng một cách đơn giản. Trước hết, sử dụng hàm đầu vào của tôi [] và các lỗi điều khiển. Ngoài ra, bạn có thể đặt ở đây một danh sách dài và chương trình này có thể xử lý nó.

# If your input value is only a number then use "Value.isdigit[] == False".
# If you need an input that is a text, you should remove "Value.isdigit[] == False".
def Input[Message]:
    Value = None
    while Value == None or Value.isdigit[] == False:
        try:        
            Value = str[input[Message]].strip[]
        except InputError:
            Value = None
    return Value

# Your answer:
def choose_colour[]:
    # Your colors list
    Colors = ["Red", "Blue", "Green"]
    # Print your menu
    print["Select colour:"]        
    for Item in Colors:
        print[str[Colors.index[Item] + 1] + ". " + Item]
    print[""]
    Choice = -1
    # If user entered an incorrect answer, ask your question again
    while Choice  len[Colors]:
        Choice = int[Input["Your choice: "]]
    # Print user choice
    print["You choosed " + Colors[Choice - 1]]

choose_colour[]

Đã trả lời ngày 24 tháng 10 năm 2017 lúc 8:13Oct 24, 2017 at 8:13

Bài Viết Liên Quan

Chủ Đề