如何使python脚本返回到它的开头?
我刚开始自学python,我做了一个基本的计算器 但是,如果用户按下了脚本上没有选项的按钮,我想让脚本转到开始,例如: “1添加” “2减法” “3乘” “4分” 选项5不存在,因此我希望在用户按“5”时,脚本打印“Error”,然后脚本返回开始,打印我上面写的选项。例如,与批处理文件中一样,如果要在执行某项操作后在脚本开始时执行,请使用: 开始 开始: (选项) 这是我的代码:如何使python脚本返回到它的开头?,python,Python,我刚开始自学python,我做了一个基本的计算器 但是,如果用户按下了脚本上没有选项的按钮,我想让脚本转到开始,例如: “1添加” “2减法” “3乘” “4分” 选项5不存在,因此我希望在用户按“5”时,脚本打印“Error”,然后脚本返回开始,打印我上面写的选项。例如,与批处理文件中一样,如果要在执行某项操作后在脚本开始时执行,请使用: 开始 开始: (选项) 这是我的代码: print ('Choose something') print ('1 Addition') print
print ('Choose something')
print ('1 Addition')
print ('2 Substract')
print ('3 Multiply')
print ('4 Divide')
option = input('Choose option');
number = int(input('First number '));
numbertwo = int(input('Second number'));
def addition (z, v) :
return z + v;
def substract (f, h,) :
return f - h;
def multiply (u, i,) :
return u * i;
def divide (n, k) :
return n / k;
if option == '1':
print(numbertwo, "+" ,numbertwo, "=", addition(numero, numerodos));
elif option == '2':
print(number, "-" ,numbertwo, "=", substract(number, numbertwo));
elif option == '3':
print(number, "*" ,numbertwo, "=", multiply(number, numbertwo));
elif option == '4':
print(number, "/" ,numbertwo, "=", divide(number, numbertwo));
elif option == '5':
print("Error")
我试着这样做,但我只是得到了打印的信息,然后脚本要求数字
你知道怎么做吗?python中没有
goto
。但是,您可以使用while
循环:
while True:
print ('Choose something')
print ('1 Addition')
print ('2 Substract')
print ('3 Multiply')
print ('4 Divide')
option = input('Choose option');
if not option in ('1','2','3','4'):
print("Error")
continue # go to the start
number = int(input('First number '));
numbertwo = int(input('Second number'));
def addition (z, v) :
return z + v;
def substract (f, h,) :
return f - h;
def multiply (u, i,) :
return u * i;
def divide (n, k) :
return n / k;
if option == '1':
print(numbertwo, "+" ,numbertwo, "=", addition(numero, numerodos));
elif option == '2':
print(number, "-" ,numbertwo, "=", substract(number, numbertwo));
elif option == '3':
print(number, "*" ,numbertwo, "=", multiply(number, numbertwo));
elif option == '4':
print(number, "/" ,numbertwo, "=", divide(number, numbertwo));
break # terminate the loop otherwise
python中没有
goto
。但是,您可以使用while
循环:
while True:
print ('Choose something')
print ('1 Addition')
print ('2 Substract')
print ('3 Multiply')
print ('4 Divide')
option = input('Choose option');
if not option in ('1','2','3','4'):
print("Error")
continue # go to the start
number = int(input('First number '));
numbertwo = int(input('Second number'));
def addition (z, v) :
return z + v;
def substract (f, h,) :
return f - h;
def multiply (u, i,) :
return u * i;
def divide (n, k) :
return n / k;
if option == '1':
print(numbertwo, "+" ,numbertwo, "=", addition(numero, numerodos));
elif option == '2':
print(number, "-" ,numbertwo, "=", substract(number, numbertwo));
elif option == '3':
print(number, "*" ,numbertwo, "=", multiply(number, numbertwo));
elif option == '4':
print(number, "/" ,numbertwo, "=", divide(number, numbertwo));
break # terminate the loop otherwise
把它放在一个循环中。请复习你的教材。“循环”是一种常见的控制流构造,在任何语言教程中都有介绍。堆栈溢出并不能取代本阅读。请继续阅读教程,了解循环。Python是在1960年和结构化编程革命之后设计的语言,没有GOTO语句。相反,您可以通过循环和条件分支,甚至递归来控制程序流。不要再去想那些“开头”的脚本了。从这些控制结构的角度来思考。把它放在一个循环中。请复习你的教材。“循环”是一种常见的控制流构造,在任何语言教程中都有介绍。堆栈溢出并不能取代本阅读。请继续阅读教程,了解循环。Python是在1960年和结构化编程革命之后设计的语言,没有GOTO语句。相反,您可以通过循环和条件分支,甚至递归来控制程序流。不要再去想那些“开头”的脚本了。考虑这些控制结构。我知道python中没有goto,这是一个例子,但我想知道当用户按数字5(选项5不存在)时,如何让脚本进入开始(选项)。因此,如果用户按5,您只想再次询问用户?如果是,请参阅我的编辑。是的,当用户按数字“5”时,我想打印类似“错误无有效选项”的内容,然后返回主菜单。编辑:非常感谢!用户被要求输入两个数字,但至少脚本开始了,谢谢!因此,在要求用户输入两个数字之前,您可能希望将支票放在开头。查看我的第二次编辑。我知道python中没有goto,这是一个示例,但我想知道当用户按数字5(选项5不存在)时,如何让脚本转到开始(选项)。因此,如果用户按5,您只想再次询问用户?如果是,请参阅我的编辑。是的,当用户按数字“5”时,我想打印类似“错误无有效选项”的内容,然后返回主菜单。编辑:非常感谢!用户被要求输入两个数字,但至少脚本开始了,谢谢!因此,在要求用户输入两个数字之前,您可能希望将支票放在开头。请参阅我的第二次编辑。