def function():
l = input("Enter the length of rectangle: ")
b = input("Enter the breadth of rectangle: ")
ask = input("Do you want to calculate area or perimeter?(a/p): ")
if ask == a or A:
area = l*b
print("Area of a rectangle with length of",l,"and breadth of",b,"is",area)
elif ask == p or P:
peri = 2*(l+b)
print("Perimeter of a rectangle with length of",l,"and breadth of",b,"is",peri)
else:
print("ERROR 404 , try again.")
function()
I am not able to get any output for the following code upon running this.
在运行此代码时,我无法获得以下代码的任何输出。
更多回答
Are you sure you don't get output that says "SyntaxError: invalid syntax" on line 13, pointing to the incorrectly indented else:
statement?
您确定您没有得到第13行显示“SynaxError:Invalid Synology”的输出,该输出指向缩进错误的Else:语句吗?
a
, A
, p
and P
are treated like variables and are not defined. You probably meant "a", "A", ...
. Also l
and b
are strings, so multiplication will not work for them.
A、A、P和P被当作变量对待,没有定义。你的意思可能是“A”,“A”,……L和b也是字符串,所以乘法对他们不起作用。
I suspect your real code doesn't have the improperly indented else:
, but has the indented call to function()
. Like that you are simply defining a function and never calling it. That would explain the lack of output.
我怀疑您的实际代码没有不正确地缩进的Else:,而是缩进了对Function()的调用。这样,您只是定义了一个函数,而从不调用它。这将解释为何产出不足。
There are several issues in your code:
您的代码中有几个问题:
You are comparing ask to variables a, A, p, and P that don't exist. Instead, you should be comparing to the string literals 'a', 'A', 'p', and 'P'.
您正在将ASK与不存在的变量a、A、p和P进行比较。相反,您应该与字符串文字‘a’、‘A’、‘p’和‘P’进行比较。
The values l and b that you receive from the input() function are strings. You need to convert them to integers or floats before performing arithmetic operations.
您从INPUT()函数接收的L和b值是字符串。在执行算术运算之前,您需要将它们转换为整数或浮点数。
The indentation of the function() call at the end of the code is incorrect. It should not be indented.
代码末尾的函数()调用的缩进不正确。它不应该缩进。
The else: block is mispositioned. You should probably align it with the if and elif blocks.
Else:块定位错误。您可能应该将其与If和Elif块对齐。
Here's a corrected version of your code:
以下是您的代码的更正版本:
def function():
l = float(input("Enter the length of rectangle: ")) # converting to float
b = float(input("Enter the breadth of rectangle: ")) # converting to float
ask = input("Do you want to calculate area or perimeter?(a/p): ")
if ask == 'a' or ask == 'A':
area = l * b
print("Area of a rectangle with length of", l, "and breadth of", b, "is", area)
elif ask == 'p' or ask == 'P':
peri = 2 * (l + b)
print("Perimeter of a rectangle with length of", l, "and breadth of", b, "is", peri)
else:
print("ERROR 404 , try again.")
function()
function()
Welcome to the Stack Overflow community!
欢迎来到堆栈溢出社区!
There are a couple of problems here. First of all, when you check to see when the player typed in a
or p
, they should be in quotation marks to show that they are strings and not variable names: "a" or "A"
.
这里有几个问题。首先,当您检查玩家何时输入a或p时,它们应该放在引号中,以表明它们是字符串,而不是变量名:“a”或“A”。
Second of all, you need to cast the length and breadth inputs to integers so that they can be operated on. Do this with l = int(input("Enter the length of the rectangle: "))
. Don't do this with ask
though because you want that to be a string!
其次,您需要将长度和宽度输入转换为整数,以便可以对它们进行操作。使用L=INT(INPUT(“输入矩形的长度:”))。不过,不要对Ask执行此操作,因为您希望它是一个字符串!
The entire else
block should also be indented 4 spaces or a tab so that it is on the same indentation level as the if
and elif
.
Also, instead of using if ask == "a" or "A"
you can do if ask.lower() == "a"
which first converts all characters to lowercase so the if
statement looks prettier.
整个Else块也应该缩进4个空格或一个制表符,以便它与IF和ELIF处于相同的缩进级别。此外,不使用if Ask==“a”或“A”,而是可以使用if ask.lower()==“a”,它首先将所有字符转换为小写,这样if语句看起来更美观。
Hope this helped,
Quizzer515SY
希望这能有所帮助,Quizzer515SY
更多回答
我是一名优秀的程序员,十分优秀!