2017-05-31 28 views
-2

因此,我一直在爲學校開發一個「AI」,並遇到了一些我不知道的東西。所以當出現錯誤時,我試圖讓它跳回到問題中,但我沒有找到答案給了我一個答案。這裏是我的代碼:如何在python中跳回幾行

# 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

不管你做什麼,或不使用這個(https://github.com/snoack/python-goto) – Eric

+0

嗯......你能不能解釋一下? –

+0

sry,我正在學習 –

回答

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 
0

這裏是一個循環的例子。當我們想重複一段代碼或直到滿足某些條件時,我們使用循環。在這種情況下,代碼將不斷要求用戶輸入數字5(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") 
0

你問這樣的事嗎?

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