[英]If user input is not part of string values asked, default output
這是我剛才寫的一段代碼,我想讓它在默認情況下調用 add function,所以如果我只是輸入回車(或任何東西),它會問我我想添加哪兩個數字。 我試過把,
else:
num1 = float(input("Enter First Number: "))
num2 = float(input("Enter Second Number: "))
print(num1, "+", num2, "=", add(num1, num2))
但這是行不通的,任何幫助將不勝感激! 這是下面的完整代碼
import math as m
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
def power(x, y):
return x ** y
def nroot(x, y):
return x ** (1/y)
def sin(D):
R = D / 180 * m.pi
return(m.sin(R))
def cos(D):
R = D / 180 * m.pi
return(m.cos(R))
print("Select Operation.")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Exponent")
print("6. sin(Degrees)")
print("7. cos(Degrees)")
while True:
# Take input from user
choice = input("Enter Choice[1-7]: ")
#check if choice is one of the four
if choice in ('1', '2', '3', '4', '5'):
num1 = float(input("Enter First Number: "))
num2 = float(input("Enter Second Number: "))
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
elif choice == '5':
print(num1, "^", num2, "=", power(num1, num2))
if choice in('6','7'):
angle = float(input('Enter Angle in degrees: '))
if choice =='6':
print('sin(',angle,') = ',sin(angle))
elif choice =='7':
print('cos(',angle,') = ',cos(angle))
break
這對我來說很好-
import math as m
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
def power(x, y):
return x ** y
def nroot(x, y):
return x ** (1/y)
def sin(D):
R = D / 180 * m.pi
return(m.sin(R))
def cos(D):
R = D / 180 * m.pi
return(m.cos(R))
print("Select Operation.")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Exponent")
print("6. sin(Degrees)")
print("7. cos(Degrees)")
while True:
# Take input from user
choice = input("Enter Choice[1-7]: ")
#check if choice is one of the four
if choice in ('1', '2', '3', '4', '5'):
num1 = float(input("Enter First Number: "))
num2 = float(input("Enter Second Number: "))
# Update:
else:
num1 = float(input("Enter First Number: "))
num2 = float(input("Enter Second Number: "))
print(num1, "+", num2, "=", add(num1, num2))
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
elif choice == '5':
print(num1, "^", num2, "=", power(num1, num2))
if choice in('6','7'):
angle = float(input('Enter Angle in degrees: '))
if choice =='6':
print('sin(',angle,') = ',sin(angle))
elif choice =='7':
print('cos(',angle,') = ',cos(angle))
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.