gpt4 book ai didi

python - 根据用户给出的答案将问题链接到更多问题 Python

转载 作者:太空宇宙 更新时间:2023-11-03 17:14:44 25 4
gpt4 key购买 nike

我一直在尝试将一个问题链接到另一个问题,例如,如果用户插入“否”,它会将您带到另一个问题。它会一直这样下去,直到找到解决方案。它就像一个故障排除程序。我的问题是,如何将一个问题链接到另一个问题?

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:
...

这是一个fully functional example 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)

这是一个fully functional example Question-id 方法的说明

关于python - 根据用户给出的答案将问题链接到更多问题 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33755272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com