首页 > 解决方案 > 我可以在python3的单行代码中一次增加2个变量相同的值吗

问题描述

我最近开始学习python。这是我的 python3 代码。如果条件满足,我需要将两个变量的值都加一。我怎样才能在一行中做到这一点,此代码工作正常。我可以在一行中使这段代码简短而甜美吗?提前致谢。

//This code works fine
if(letters[index] == letters[index+1]):
    count += 1
    index += 1

是否可以在 1 行中执行此操作。我的意思是这样的 [count,index]+=1(这不起作用)

标签: pythonpython-3.x

解决方案


你可以做

if(letters[index] == letters[index+1]):
    count,index=count+1,index+1

或者

if(letters[index] == letters[index+1]):
    count+=1;index+=1

推荐阅读