-2

So, I've been working on an "AI" for school, and I ran into something I didn't know. So when there was an error, I tried to make it jump back to the question, but nothing I searched for gave me an answer. Here is my code:

# 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?")`
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

3 Answers3

1

I'm not sure what you mean by jumping back to the question, but you could easily implement a while loop.

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")

This will simply loop through until the user types 'no'. The chat portion would be easiest if implemented in another while loop after this one. Once the user answers no the maths question, you can start a new loop for chat:

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

Here is an example of a loop. We use loops when we want to repeat sections of code again and again or until a certain condition is met. In this case the code will keep asking the user to input the number five (5), until they get it right.

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")
scotty3785
  • 6,763
  • 1
  • 25
  • 35
0

Are you asking something like this?

# 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!")
flag=0
while flag!=1:
    maths = input("Do you wanna do maths? \n").lower()
    if (maths == "yes") or (maths == "yeah") or (maths == "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))
            flag=1
        elif symbol == "-":
            output = (int(number1) - int(number2))
            flag=1
        elif symbol == "*":
            output = (int(number1) * int(number2))
            flag=1
        elif symbol == "/":
            output = (int(number1) / int(number2))
            flag=1
        else:
            print ("Something went wrong, ty again")
            flag=0
        time.sleep(5) 
        print (output)
    else:
        print ("Would you like to chat then?")
Vijayabhaskar J
  • 417
  • 2
  • 4
  • 17