【急】【Python入门】 作业纠错 (为啥显示syntax error)?题目如图 我写的代码如下

图片说明

那个啥 图片照错了 题目里面没有说要用 “%f” 求给个不用"%f"的码。
"Your code will be provided an unknown value for x. (That is, don't specify x yourself.)

Write an if/elif/else statement which implements this piecewise expression and prints the result in the format 'x N'. Assume that the provided value is called x."

我写的代码如下:

if x < -1:
    x_N = 0
    print (x_N)
elif x>= -1 & x<=0 :
    x_N = 1+x
    print (x_N)
elif x>= 0 & x<=1 :
    x_N = 1-x
    print (x_N)
else:
    x_N = 0
    print (x_N)

elif x>= -1 & x<=0 :
这里用 and 而不是 &
别的地方类似

x使用前必须声明,是在哪声明的?

你的x没有定义,在使用前要先对x赋值

使用格式的代码如下,保留6位小数

# x = -0.250000

if x < -1:
    x_N = 0
elif -1 <= x <= 0:
    x_N = 1 + x
elif 0 <= x <= 1:
    x_N = 1 - x
else:
    x_N = 0
str1 = "%.6f %.6f" % (x, x_N)
print(str1)

没有格式话代码如下

# x = -0.250000

if x < -1:
    x_N = 0
elif -1 <= x <= 0:
    x_N = 1 + x
elif 0 <= x <= 1:
    x_N = 1 - x
else:
    x_N = 0
str1 = "{0} {1}".format(x, x_N)
print(str1)

楼主按需使用吧