Elif 和 if 语句无法正常工作
Elif and if statement not working in and out of function
代码:
class Car:
y = 0
x = 0
x_miles = y / 5280
y_miles = x / 5280
direction = 0
speed = 100
fps = speed * 1.467
Cords = {x}, {y}
Cords_miles = x_miles, y_miles
def driving(self):
drv = 'start'
while drv != 'stop':
if Car.direction == -270:
Car.direction = 90
elif Car.direction == 270:
Car.direction = -90
elif Car.direction == -180:
Car.direction = 180
drv = input('Say W(Forward), S(Stop), A(TurnLeft), D(TurnRight), C(GetCords): ').lower
if drv == 's':
print('this never gets executed, as well as the elifs statements after')
else:
print('this always gets executed')
问题是当我调用函数然后输入一个输入时,例如,如果我给它字母s,它会立即跳到else语句。这是我输入 s:
时的输出
Say W(Forward), S(Stop), A(TurnLeft), D(TurnRight), C(GetCords): s
this always gets executed
我似乎无法修复它,所以如果有人能提供帮助就太好了!
您 .lower
后缺少括号。如您所见 here,没有括号,您只是设置对 lower 方法的引用但实际上并未调用它。
代码:
class Car:
y = 0
x = 0
x_miles = y / 5280
y_miles = x / 5280
direction = 0
speed = 100
fps = speed * 1.467
Cords = {x}, {y}
Cords_miles = x_miles, y_miles
def driving(self):
drv = 'start'
while drv != 'stop':
if Car.direction == -270:
Car.direction = 90
elif Car.direction == 270:
Car.direction = -90
elif Car.direction == -180:
Car.direction = 180
drv = input('Say W(Forward), S(Stop), A(TurnLeft), D(TurnRight), C(GetCords): ').lower
if drv == 's':
print('this never gets executed, as well as the elifs statements after')
else:
print('this always gets executed')
问题是当我调用函数然后输入一个输入时,例如,如果我给它字母s,它会立即跳到else语句。这是我输入 s:
时的输出Say W(Forward), S(Stop), A(TurnLeft), D(TurnRight), C(GetCords): s
this always gets executed
我似乎无法修复它,所以如果有人能提供帮助就太好了!
您 .lower
后缺少括号。如您所见 here,没有括号,您只是设置对 lower 方法的引用但实际上并未调用它。