猿问

我的 else 不工作,但没有显示任何错误标志。如果,elif 工作正常。python是我的代码

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 代码不显示。


FFIVE
浏览 143回答 4
4回答

泛舟湖上清波郎朗

您需要包含圆括号来调用函数:else:     wrong_sign()

一只斗牛犬

在最后一行,你必须调用wrong_sign()函数:else:     wrong_sign()

湖上湖

您应该更改您的 elif 语句之一:elif sign == "*" or sign == "x":

慕桂英3389331

这是您的正确代码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()无效符号的输出Calculator for substraction, addition, multiplication and divisionEnter a sign:$Enter a number:2Enter a second number:3Sign entered is invalid please use one of those 1. - for substraction  2. + for addition  3. x or * for multiplication  4. / for devision
随时随地看视频慕课网APP

相关分类

Python
我要回答