Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用条件语句、python、pyscripter将变量更改为等于其他变量_Python - Fatal编程技术网

如何使用条件语句、python、pyscripter将变量更改为等于其他变量

如何使用条件语句、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

我想编码:如果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 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)