Python 3.x 正在删除变量以不将其打印出来
所以我正在制作我的第一台计算器Python 3.x 正在删除变量以不将其打印出来,python-3.x,printing,Python 3.x,Printing,所以我正在制作我的第一台计算器 import math # Adding two numbers def add(num1, num2): return num1 + num2 #Substract two numbers def subst(num1, num2): return num1 - num2 #Multiply two numbers def multi(num1, num2): return num1 * num2 #Divide two numb
import math
# Adding two numbers
def add(num1, num2):
return num1 + num2
#Substract two numbers
def subst(num1, num2):
return num1 - num2
#Multiply two numbers
def multi(num1, num2):
return num1 * num2
#Divide two numbers
def divide(num1, num2):
return num1 / num2
#Exponentiation of number
def expont(num1, num2):
return pow(num1, num2)
#Square root
def sqrt(num1):
return math.sqrt(num1)
#Sin
def sin(num1):
return math.sin(num1)
#Cosinus
def cos(num1):
return math.cos(num1)
#Tangent
def tan(num1):
return math.tg(num1)
print("Select your operation: \n"
"1.Add\n"
"2.Substract\n"
"3.Multiply\n"
"4.Divide\n"
"5.Exponentiation\n"
"6.Square root\n"
"7.Sine\n"
"8.Cosine\n"
"9.Tangent")
operation = input("Select your operation 1, 2, 3, 4, 5, 6, 7, 8 ,9: ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if operation == "1":
print(add(num1,num2))
elif operation == "2":
print(subst(num1,num2))
elif operation == "3":
print(multi(num1,num2))
elif operation == "4":
print(divide(num1, num2))
elif operation == "5":
print(expont(num1,num2))
elif operation == "6":
print(sqrt(num1))
elif operation == "7":
print(sin(num1))
elif operation == "8":
print(cos(num1))
else:
print(tan(num1))
但是当我尝试正弦、余弦和tanget时,我遇到了一个问题。正如你们看到的,我只想取一个值,即num1,但程序要求我输入num2。
如何防止或可能禁用/删除操作>=7的num2
我尝试过使用if、while语句以及del。但它不起作用。
对于大于或等于7的操作,我应该如何禁用num2变量?只需将第二个输入封装在if语句中,如
if operation not in'6789':
,它就不会在最后4个操作中执行。谢谢@Axe319!这对我很有帮助,程序也能正常工作:)谢谢,只需将您的第二次输入封装在一个if语句中,如if操作不在“6789”中:
,它就不会在您的最后4次输入中执行。谢谢@Axe319!这对我很有帮助,程序也如期运行:)谢谢