# 程序006.01cond1 =Truecond2 =Truecond3 =Truecond4 =Truecond5 =Truecond6 =Trueif cond1 and cond2 and cond3 and cond4 and cond5:print('Bam!')
if有5个条件,如果编辑器显示不下我们会想把条件折行输入,这样写会出错
# 程序006.02cond1 =Truecond2 =Truecond3 =Truecond4 =Truecond5 =Truecond6 =Trueif cond1 and cond2 and cond3 and cond4 and cond5:print('Bam!')
因为Python的代码块逻辑被打断了
要想折行,只需要在代码行最后加上\,告诉Python下一行续写这一行的代码
# 程序006.03cond1 =Truecond2 =Truecond3 =Truecond4 =Truecond5 =Truecond6 =Trueif cond1 and cond2 and cond3 and cond4 \and cond5:# 这样写等价于程序006.01print('Bam!')
整数变量
前面我们介绍了字符串变量,现在我们再介绍下整数变量
i =1# 我是一个整数变量
顾名思义,整数变量就是存放整数的容器,在这个变量上,我们可以做各种算数操作
# 程序006.04i =1print(i)# 打印1i = i +1# 还有一个简化写法是 i += 1print(i)# 打印2i = i -1# 还有一个简化写法是 i -= 1print(i)# 打印1print(i <2)# 打印True
让我们把这首情诗改成完整的Python程序
# 程序006.05mountain_without_peaks =Falseriver_runs_dry =Falsewinter_thunder_rolls =Falsesummer_rain_turns_to_snow =Falseheaven_earth_merged =Falselife =18whilenot mountain_without_peaks andnot river_runs_dry andnot winter_thunder_rolls\andnot summer_rain_turns_to_snow andnot heaven_earth_merged and life <100:print(f'{life} years old: I love you!') life +=1print('I love you forever...')