簡體   English   中英

根據用戶給出的答案將Qs鏈接到更多Qs

[英]Linking Qs to more Qs based on answers the user gives Python

我一直試圖將一個問題與另一個問題聯系起來,例如,如果用戶插入否,它將帶您進入另一個問題。 在找到解決方案之前,它將一直這樣。 這就像一個故障排除程序。 我的問題是,如何將一個問題鏈接到另一個問題?

answer1 = "yes"
answer2 = "no"
question = "did you drop your phone?"
print(question)
guess = input().lower()
name = input("Please enter your answer: ")
if guess == answer1:
    print("I will ask you new question")
if guess == answer2:
    print("Next question")
else:
    print("Wrong")

我得到了代碼的答案,但我想要一個更簡單的代碼版本。 例如,ID代碼對我來說有點難,所以有人可以在這里用此代碼編寫完整的代碼嗎?

碼:

These are all my solutions from solution2 to sol8. Sol8 means solution 8.   The code whithin the red comment hashtags are all the solutions except the print    function which is to start off the program.

print("Welcome to the troubleshooting program.Please answer 'yes' or 'no' to  all questions.")##
solution2 = "Please get a screen replaced."
solution1 = "Your device is still under warranty"#
solution = "Reset your phone"
sol0 = "Delete some apps, you need atleast 5 GB of memory to run your phone properly."
sol1 = "Take out everything from the phone(battery,SIM card etc.)and place  the phone in a bowl of rice.\nLeave it there for about 24 hours to let the rice   absorb the water"#
sol2 = "Charge your phone fully and switch it on"
sol3 = "Keep holding the power on button until the screen turns on, if not then contact the phone provider for a replacement."#
sol4 = "You need a screen replacement. Get it fixed"
sol5 = "Get a screen replacement or contact your phone provider for a  replacement."#
sol6 = "You need to update your phone software and apps."
sol7 = "Take the battery out, and put it back in. Then press the power on button until your phone switches on."#
sol8 = "You dont need to do anything. Your phone doesn't have any problems."

if input("Did you  buy your phone recently? ") == 'yes':
if input("Did you drop your phone? ") == 'yes':
    if input("Did it become wet when you dropped it? ") == 'yes':
     print(sol1)
else:  
    if input("Is the phone fully charged? ") == 'yes':
        if input("Is the screen on?") == 'yes':
            print(sol3)
    else:

        print(sol2) 
else:  
    if input("Has your phone ever been too slow? " ) == 'yes':
    print(solution)
    else:  
        if input("Have you got more than 30 apps? ") == 'yes':
        print(sol0)
        else:
            print (sol8)

就我現在所能想到的,您有兩個選擇。

如果情況

您只需定義所有可能的路徑。 如果沒有太多問題,這可能是最佳選擇。

guess = input("Did you drop your phone? ")
if guess == "yes":
    guess2 = input("Did it shatter the screen? ")
    if guess2 == "yes":
        ...
    else:
        ...
else:  # assuming the user entered 'no' if the answer was not 'yes'
    guess2 = input("Did your phone come in contact with water? ")
    if guess2 = "yes":
        ...
    else:
        ...

這是if-case方法的完整功能示例

預定義的問題ID

您還可以給每個問題一個唯一的ID(例如數字),將其放在函數中,然后從那里繼續。

def new_question(id):
    if id == 1:
        return question1()
    elif id == 2:
        return question2()
    elif id == 3:
        return question3()
    elif id == 4:
        return question4()
    ... (more ids)

def question1():
    guess = input("Did you drop your phone? ")
    if guess == "yes":
        return 2  # next question will be question 2
    else:
        return 3  # next question will be question 3

def question2():
    guess = input("Did it shatter the screen? ")
    if guess == "yes":
        return 4  # next question will be question 4
    else:
        return 5  # next question will be question 5

def question3():
    guess = input("Did your phone come in contact with water? ")
    if guess == "yes":
        return 4  # next question will be question 4
    else:
        return 7  # next question will be question 7

... (more questions)

answer = 1
while answer != 0:
    answer = new_question(answer)

這是問題ID方法的完整功能示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM