Python中的if-elif-else语句工作不正常
这是我的密码:Python中的if-elif-else语句工作不正常,python,python-3.x,if-statement,Python,Python 3.x,If Statement,这是我的密码: def moveEntity(entity): print('forward=1') print('backward=2') print('left=3') print('right=4') direction = input('Where would you like to move?') distance = input('How many units?') if direction == 1: ent
def moveEntity(entity):
print('forward=1')
print('backward=2')
print('left=3')
print('right=4')
direction = input('Where would you like to move?')
distance = input('How many units?')
if direction == 1:
entity.y = entity.y + distance
elif direction == 2:
entity.y = entity.y - distance
elif direction == 3:
entity.x = entity.x - distance
elif direction == 4:
entity.x == entity.x + distance
else:
print('invalid input')
当我运行此函数并输入4个选项(1,2,3,4)中的任何一个时,该函数总是跳过4个if/elif语句并执行else语句。我无法找出我上面发布的代码有什么问题。我试着在输入变量“方向”和“距离”后打印它们的值,它们都打印为正确的值。之后,尽管运行了if和elif语句,else语句仍然被执行。任何帮助都将不胜感激 这是因为它返回一个字符串,因此您需要将输入转换为整数以与该数字进行比较,或者只与字符串数字进行比较,还请注意,在将其放入计算之前,您需要将距离转换为整数:
def moveEntity(entity):
print('forward=1')
print('backward=2')
print('left=3')
print('right=4')
direction = input('Where would you like to move?')
while True:
try :
distance = int(input('How many units?'))
if direction == '1':
entity.y = entity.y + distance
elif direction == '2':
entity.y = entity.y - distance
elif direction == '3':
entity.x = entity.x - distance
elif direction == '4':
entity.x == entity.x + distance
#return value
except ValueError::
print('please enter a valid digit')
请注意,当您将输入转换为int
时,可能会引发值错误,因此为了处理此问题,您可以使用try-except表达式。正是因为它返回一个字符串,因此您需要将输入转换为整数以与该数字进行比较,或者仅与字符串数字进行比较,还请注意,在将距离放入计算之前,需要将距离转换为整数:
def moveEntity(entity):
print('forward=1')
print('backward=2')
print('left=3')
print('right=4')
direction = input('Where would you like to move?')
while True:
try :
distance = int(input('How many units?'))
if direction == '1':
entity.y = entity.y + distance
elif direction == '2':
entity.y = entity.y - distance
elif direction == '3':
entity.x = entity.x - distance
elif direction == '4':
entity.x == entity.x + distance
#return value
except ValueError::
print('please enter a valid digit')
请注意,当您将输入转换为
int
时,可能会引发值错误,因此为了处理此问题,您可以使用try-except表达式。这里有两个问题。第一个,正如其他答案所指出的,是将int
与字符串进行比较。因此,用int
包装您的输入。第二个问题是,您在上一次赋值中有==
,因此即使它达到了这种情况,它也不会更新entity.x
的值。此代码应适用于:
def moveEntity(entity):
print('forward=1')
print('backward=2')
print('left=3')
print('right=4')
direction = int(input('Where would you like to move?'))
distance = int(input('How many units?'))
if direction == 1:
entity.y = entity.y + distance
elif direction == 2:
entity.y = entity.y - distance
elif direction == 3:
entity.x = entity.x - distance
elif direction == 4:
entity.x = entity.x + distance
else:
print('invalid input')
这里有两个问题。第一个,正如其他答案所指出的,是将int
与字符串进行比较。因此,用int
包装您的输入。第二个问题是,您在上一次赋值中有==
,因此即使它达到了这种情况,它也不会更新entity.x
的值。此代码应适用于:
def moveEntity(entity):
print('forward=1')
print('backward=2')
print('left=3')
print('right=4')
direction = int(input('Where would you like to move?'))
distance = int(input('How many units?'))
if direction == 1:
entity.y = entity.y + distance
elif direction == 2:
entity.y = entity.y - distance
elif direction == 3:
entity.x = entity.x - distance
elif direction == 4:
entity.x = entity.x + distance
else:
print('invalid input')
这是因为输入是字符串,但if循环中的值是整数
坏的:
好:
这是因为输入是字符串,但if循环中的值是整数
坏的:
好:
在添加前,您不需要将距离转换为整数吗?在添加前,您不需要将距离转换为整数吗?案例4的更新使用的是==
,而不是=
,因此它将比较entity.x,而不是为其分配新值。案例4的更新使用=
,不是=
,因此它将比较entity.x,而不是为其分配新值。