[英]My else is not working but doesn't show any error sign. If, elif is working perfectly. python is my code
print("Calculator for substraction, addition, multiplication and division")
sign=input("Enter a sign:")
num1=float(input("Enter a number:"))
num2=float(input("Enter a second number:"))
def wrong_sign():
print("Sign entered is invalid please use one of those 1. - for substraction \n 2. + for addition \n 3. x or * for multiplication \n 4. / for devision")
if sign == "-":
print(num1-num2)
elif sign == "+" :
print(num1+num2)
elif sign == "/":
print(num1/num2)
elif sign == "*" or "x":
print(num1 * num2)
else:
wrong_sign
仅当我尝试输入无效符号时,没有代码错误,因为用户什么也没有出现,并且 wrong_sign 代码不显示。
您需要包含圆括号才能调用 function:
else:
wrong_sign()
您应该更改您的 elif 语句之一:
elif sign == "*" or sign == "x":
在最后一行,你必须调用wrong_sign()
function:
else:
wrong_sign()
这是您的正确代码
print("Calculator for substraction, addition, multiplication and division")
sign=input("Enter a sign:")
num1=float(input("Enter a number:"))
num2=float(input("Enter a second number:"))
def wrong_sign():
print("Sign entered is invalid please use one of those 1. - for substraction \n 2. + for addition \n 3. x or * for multiplication \n 4. / for devision")
if sign == "-":
print(num1-num2)
elif sign == "+" :
print(num1+num2)
elif sign == "/":
print(num1/num2)
elif sign == "*" or sign == "x":
print(num1 * num2)
else:
wrong_sign()
Output 为无效符号
Calculator for substraction, addition, multiplication and division
Enter a sign:$
Enter a number:2
Enter a second number:3
Sign entered is invalid please use one of those 1. - for substraction
2. + for addition
3. x or * for multiplication
4. / for devision
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.