Hướng dẫn if else exit python - nếu khác thoát khỏi python

Đây là một chương trình trả lời trò chuyện và vấn đề tôi hiện đang gặp phải là tôi cố gắng thoát ra nếu câu trả lời đầu vào không khớp với một trong những câu trả lời của tôi, nhưng vì tất cả các câu trả lời của ba trong số các câu hỏi được đặt ra như vậy Ngay cả quyền cho câu hỏi mà nó chọn ngẫu nhiên đã được nhập, Python vẫn nghĩ rằng nó không đúng vì nó không đáp ứng hai người kia và sau đó nó sẽ thoát ra. Vui lòng cho tôi biết làm thế nào tôi có thể thực hiện một cách tiếp cận khác về các chương trình này. Cảm ơn sự trợ giúp1

import random

x=input["What is your name? "]

def main[]:

    def feeling[]:
        return input["How are you feeling right now " +str[x]+"? "]

    def homesick[]:
        return input["Do you miss your home? "]

    def miss[]:
         return input["Who do you miss?"]

    prompts = [feeling,homesick,miss]
    response = random.choice[prompts][]
     
    if response==["tired"]:
                    Tired=['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
                    print[random.choice[Tired]]
    else:
        exit[]

    if response==["yes"]:
                    yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
                    print[random.choice[yes]]
    else:
        exit[]

    if response==["my mom"]:
                    print["Mom will be in town soon"]
    else:
        exit[]

    main[]

main[]

4b0

20.8k30 Huy hiệu vàng92 Huy hiệu bạc137 Huy hiệu đồng30 gold badges92 silver badges137 bronze badges

Hỏi ngày 10 tháng 1 năm 2018 lúc 14:49Jan 10, 2018 at 14:49

Python cung cấp cấu trúc điều khiển dòng chảy đẹp cho điều này bằng cách sử dụng Elif. Xem tài liệu chính thức tại đây: //docs.python.org/3/tutorial/controlflow.html

Vì vậy, ý tưởng là tất cả các tuyên bố được đánh giá như một đơn vị.

if response==["tired"]:
                Tired=['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
                print[random.choice[Tired]]

elif response==["yes"]:
                yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
                print[random.choice[yes]]

elif response==["my mom"]:
                print["Mom will be in town soon"]
else:
    exit[]

Bây giờ các tuyên bố sẽ chạy cho đến khi một là True. Nếu tất cả là false, nó sẽ mặc định cho câu lệnh else.

Đã trả lời ngày 10 tháng 1 năm 2018 lúc 14:52Jan 10, 2018 at 14:52

Pastalegpastalegpastaleg

1.7422 Huy hiệu vàng17 Huy hiệu bạc22 Huy hiệu đồng2 gold badges17 silver badges22 bronze badges

Thay vì

if
else: exit

if
else: exit

if
else: exit

làm cái này:

if

elif:

elif:

else: exit

Bởi vì nếu không, nếu 1ST ____1010is sai, bạn sẽ thoát và không kiểm tra những người khác.

Đã trả lời ngày 10 tháng 1 năm 2018 lúc 14:53Jan 10, 2018 at 14:53

KYL3RKYL3RKYL3R

3,3861 Huy hiệu vàng9 Huy hiệu bạc23 Huy hiệu đồng1 gold badge9 silver badges23 bronze badges


import random

name = input["What is your name? "]

def main[]:

    def feeling[]:
        response = input["How are you feeling right now {name}?".format[name=name]]
        if response == "tired":
            tired = ['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
            print[random.choice[tired]]
        else:
            exit[]

    def homesick[]:
        response = input["Do you miss your home? "]
        if response == "yes":
            yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
            print[random.choice[yes]]
        else:
            exit[]

    def miss[]:
         response = input["Who do you miss?"]
         if response == "my mom":
             print["Mom will be in town soon"]
         else:
             exit[]

    prompts = [feeling, homesick, miss]
    random.choice[prompts][]

    main[]

Đã trả lời ngày 10 tháng 1 năm 2018 lúc 15:07Jan 10, 2018 at 15:07

Rafał łużyńskirafał łyńskiRafał Łużyński

6.7335 huy hiệu vàng25 Huy hiệu bạc38 Huy hiệu đồng5 gold badges25 silver badges38 bronze badges

0

Bạn nên nhóm các lời nhắc và câu trả lời và trả lời dự kiến ​​của chatbot, ví dụ: như các bộ dữ liệu ở dạng

if response==["tired"]:
                Tired=['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
                print[random.choice[Tired]]

elif response==["yes"]:
                yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
                print[random.choice[yes]]

elif response==["my mom"]:
                print["Mom will be in town soon"]
else:
    exit[]
1

feeling = ["How are you feeling right now " +str[x]+"? ", "tired",
           ['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']]
miss = ...
homesick = ...

prompts = [feeling,homesick,miss]
choice = random.choice[prompts][]

prompt, ans, replies
resp = input[promp]
if resp == ans:
    print[random.choice[replies]]
else:
    exit[]

Điều này cũng sẽ giúp việc mở rộng chatbot của bạn dễ dàng hơn với nhiều cặp câu hỏi/câu trả lời hơn hoặc cũng để cung cấp các câu trả lời chatbot khác nhau cho các câu trả lời khác nhau của người dùng.

Đã trả lời ngày 10 tháng 1 năm 2018 lúc 14:59Jan 10, 2018 at 14:59

tobias_ktobias_ktobias_k

79.5K11 Huy hiệu vàng115 Huy hiệu bạc172 Huy hiệu đồng11 gold badges115 silver badges172 bronze badges

Không hiểu mã của bạn, nhưng bạn có thể làm điều gì đó như:

if response!=["tired"]:
    exit[]
    return


Tired=['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
print[random.choice[Tired]]

if response!=["yes"]:
    exit[]
    return

yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
print[random.choice[yes]]

[...]

Đã trả lời ngày 10 tháng 1 năm 2018 lúc 15:15Jan 10, 2018 at 15:15

Bài Viết Liên Quan

Chủ Đề