如何使用条件语句、python、pyscripter将变量更改为等于其他变量
我想编码:如果cc是负数,那么cc应该变成0。cc是一个变量。我该怎么做? 插入的代码只是我代码的一部分如何使用条件语句、python、pyscripter将变量更改为等于其他变量,python,Python,我想编码:如果cc是负数,那么cc应该变成0。cc是一个变量。我该怎么做? 插入的代码只是我代码的一部分 if cc <0 : then cc == 0 print("The skill you have inputted is negative, it will stored as 0") elif dd <0 : then dd == 0 print ("The skill you have inputted is negative, it wi
if cc <0 :
then cc == 0
print("The skill you have inputted is negative, it will stored as 0")
elif dd <0 :
then dd == 0
print ("The skill you have inputted is negative, it will be stored as 0")
else :
print ("Don't worry nothing has changed!")
如果cc只需使用=
将变量分配给一个新值:
if cc < 0:
cc = 0
print("The skill you have inputted is negative, it will stored as 0")
elif dd < 0:
dd = 0
print("The skill you have inputted is negative, it will be stored as 0")
else:
print("Don't worry nothing has changed!")
使用
同时避免使用if
语句。因此,如果cc
大于0,则无论dd
的值是什么?dd是相同的,如果dd为负,则dd应为0,然后您不应将该条件放入elif
子句中…非常正确,更改了它,非常感谢您的帮助(:当你不理解一个代码错误时,这太烦人了!!!@user3569687-不,你需要使用=
进行变量赋值。=
用于比较测试。你应该看看我在答案底部给出的代码。我相信它可以满足你的所有要求。
if cc < 0:
cc = 0
print("The skill you have inputted is negative, it will stored as 0")
elif dd < 0:
dd = 0
print("The skill you have inputted is negative, it will be stored as 0")
else:
print("Don't worry nothing has changed!")
>>> cc = -1
>>> if cc < 0:
... cc = 0
...
>>> cc
0
>>>
if cc < 0 or dd < 0: # See if cc or dd is less than 0
if cc < 0: # See if cc is less than 0
cc = 0 # Assign cc to 0
print("The skill you have inputted is negative, it will stored as 0")
if dd < 0: # See if dd is less than 0
dd = 0 # Assign dd to 0
print("The skill you have inputted is negative, it will stored as 0")
else: # If we get here, then neither cc nor dd was less than 0
print("Don't worry nothing has changed!")
cc = max(0, cc)