如何在python中跳回几行

时间:2017-05-31 16:15:23

标签: python python-3.x

所以,我一直在为学校做一个“AI”,我遇到了一些我不知道的事情。因此,当出现错误时,我试图让它跳回到问题,但我搜索的任何内容都没有给我答案。这是我的代码:

# CodeOne's AI
# AI
number1 = 0
number2 = 0
symbol = 0
output = 0
import time

print ("Hello and welcome to CodeOne's miniature AI")
name = input("Enter your name \n")
print ("Well hello,",name + ".")
print ("I am ROB (Recognising Organised Robot) it's nice to meet you!")
print ("I can do many things, such as do maths equations, have small conversations, and more!")
maths = input("Do you wanna do maths? \n")
if maths == "yes" or "Yes" or "YES" or "Yeah" or "yeah" or "YEAH" or "yep" or "Yep" or "YEP":
    print ("Ok, type away")
    number1 = input("Input the first number \n")
    symbol = input("Input your desired operation (+,-,*,/) \n")
    number2 = input("Input the second number \n")
    if symbol == "+":
        output = (int(number1) + int(number2))
    elif symbol == "-":
        output = (int(number1) - int(number2))
    elif symbol == "*":
        output = (int(number1) * int(number2))
    elif symbol == "/":
        output = (int(number1) / int(number2))
    else:
        print ("Something went wrong, ty again")       
    time.sleep(5) 
    print (output)
else:
    print ("Would you like to chat then?")`

3 个答案:

答案 0 :(得分:1)

通过回到问题我不确定你的意思,但你可以很容易地实现while循环。

number1 = 0
number2 = 0
symbol = 0
output = 0
import time

print ("Hello and welcome to CodeOne's miniature AI")
name = input("Enter your name \n")
print ("Well hello,",name + ".")
print ("I am ROB (Recognising Organised Robot) it's nice to meet you!")
print ("I can do many things, such as do maths equations, have small conversations, and more!")
maths = input("Do you wanna do maths? \n")
while (maths.lower() != "no"):
    print ("Ok, type away")
    number1 = input("Input the first number \n")
    symbol = input("Input your desired operation (+,-,*,/) \n")
    number2 = input("Input the second number \n")
    if symbol == "+":
        output = (int(number1) + int(number2))
    elif symbol == "-":
        output = (int(number1) - int(number2))
    elif symbol == "*":
        output = (int(number1) * int(number2))
    elif symbol == "/":
        output = (int(number1) / int(number2))
    else:
        print ("Something went wrong, ty again")       
    time.sleep(5) 
    print (output)
    maths = input("Do you wanna do maths? \n")

这将直接循环直到用户输入“no”。如果在此之后的另一个while循环中实现,聊天部分将是最简单的。一旦用户没有回答数学问题,你就可以开始一个新的聊天循环:

chat = input("Would you like to chat then?" )
while (chat.lower() != "no"):
#do something

答案 1 :(得分:0)

这是一个循环的例子。当我们想要一次又一次地重复代码段或者直到满足某个条件时,我们使用循环。在这种情况下,代码将继续要求用户输入数字五(5),直到他们正确。

answer = 'wrong'

while answer == 'wrong':
    user_input = input("Input the number five: ")
    if user_input == '5'
        answer = 'correct'
        print("Good job!")
    else:
        print("WRONG try again!")

print("Game over man")

答案 2 :(得分:0)

你问这样的事吗?

select task.task_id, task.project_id, task.priority_type_id
from task 
where task.project_id in (select project_id from project where completion < 100)