题目
题目

Fal25_CS003C_74229 Ch. 5 Pre-Quiz (Remotely Proctored)

单项选择题

The following function is supposed to return -1 when x is negative, +1 when x is positive, or 0 if x is zero. What, if anything, is wrong with the function? def plusMinusZero(x) : if x == 0 : return 0 elif x <= 0 : return -1 elif x >= 0 : return 1

选项
A.The <= and >= must be replaced with < and >
B.Both occurrences of elif must be replaced with if
C.A return statement must be added at the end of the function
D.nothing is wrong with the function
查看解析

查看解析

标准答案
Please login to view
思路分析
Question restatement: We are given a Python function plusMinusZero(x) intended to return -1 for negative x, +1 for positive x, and 0 when x is zero. We must evaluate what, if anything, is wrong with the function and analyze each answer option. Option 1: The <= and >= must be replaced with < and >. - This would change the logic. With the current code, when x is negative, the first condition x == 0 is false, then x <= 0 becomes true and correctly returns -1. Replacing <= with < would cause negative values to fail that bran......Login to view full explanation

登录即可查看完整答案

我们收录了全球超50000道考试原题与详细解析,现在登录,立即获得答案。

更多留学生实用工具

加入我们,立即解锁 海量真题独家解析,让复习快人一步!