是否有可能使程序在运行后返回其原始状态。例如,我在下面做了一个计算器脚本。是否可以使它在输出答案后重新开始,而不是用户必须退出脚本并再次运行它。 代码:
class bcolors:
Red = '\033[91m'
Green = '\033[92m'
Blue = '\033[94m'
Cyan = '\033[96m'
White = '\033[97m'
Yellow = '\033[93m'
Magenta = '\033[95m'
Grey = '\033[90m'
Black = '\033[90m'
Default = '\033[99m'
#code for addition
def addition(x, y):
return x + y
#code for subtraction
def subtraction(x, y):
return x - y
#code for multiplication
def multiplication(x, y):
return x * y
#code for division
def division(x, y):
return x / y
print(bcolors.Blue + "Hey and welcome to my first python program")
print("This is a calculater I made in my spare time!")
print(bcolors.Magenta + "Simply type in your question here and you will get answer.")
print(bcolors.Red + "This program is made by and credited to Mohammad Al Nesef")
print(bcolors.Yellow + "1.addition")
print(bcolors.Yellow + "2.subtraction")
print(bcolors.Yellow + "3.multiplication")
print(bcolors.Yellow +"4.division")
choice = input(bcolors.Green + "Enter choice(1/2/3/4: ")
num1 = input(bcolors.Blue + "Enter first number: ")
num2 = (input(bcolors.Magenta + "Enter second number: "))
if choice == '1':
print(float(num1), "+", float(num2), "=", addition(num1,num2))
elif choice == "2":
print(float(num1), "-", float(num2), "=", subtraction(num1,num2))
elif choice == "3":
print(float(num1), "*", float(num2), "=", multiplication(num1,num2))
elif choice == "4":
print(float(num1), "/", float(num2), "=", division(num1,num2))
else:
print("Invalid Input")
答案 0 :(得分:0)
您没有在此行中关闭括号:
num2 = (input("Enter second number: ")
还请注意对文件使用相同的缩进。 PEP8符合性意味着有4个空格。
答案 1 :(得分:0)
实际上,您不应该在第35行的“输入”之前给一个多余的括号
Column(
children: <Widget>[
Padding(padding: EdgeInsets.fromLTRB(10, 0, 10, 20)),
Text('${e.phrase}', style: TextStyle(color: Colors.white60),),
Divider(height: 5, color: Colors.transparent,),
Text('${e.author}', style: TextStyle(color: Colors.white60),)
],
),
实际代码应为:
num2 =(input("Enter second number: ")
答案 2 :(得分:0)
如果您正在寻找脚本本身的硬重启,而不希望通过更多的行来优雅地处理它,那么当您要重新开始时,请调用以下函数:
def restart_calc_script():
import sys
print("resetting the calculator.")
import os
os.execv(sys.executable, ['python'] + sys.argv)