[英]how to change a variable to equal somehting else using conditional statements, python, pyscripter
I want to code for: if cc is negative, then cc should become 0. cc is a variable.我想编码:如果 cc 是负数,那么 cc 应该变成 0。cc 是一个变量。 how do I do that?
我怎么做? the code inserted is only a section of my code.
插入的代码只是我代码的一部分。
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!")
is this correct now???现在正确了吗???
if cc <0 or dd <0:
cc == 0
dd ==0
print("The skill you have inputted is negative, it will stored as 0")
else :
print ("Don't worry nothing has changed!")
if c==0 or d==0:
print("The strength you inputted is 0. Your character dies.")
elif c<0 or d<0:
print("The strength you have inputted is negative. Your character dies.")
else :
print ("Don't worry nothing has changed!")
Just assign the variable to a new value with =
:只需使用
=
将变量分配给新值:
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!")
See a demonstration below:请参阅下面的演示:
>>> cc = -1
>>> if cc < 0:
... cc = 0
...
>>> cc
0
>>>
However, your code looks like it has a logic error too.但是,您的代码看起来也有逻辑错误。 If
cc
is greater than zero, dd
will never be checked because you used elif
.如果
cc
大于零,则永远不会检查dd
因为您使用了elif
。
Perhaps you want something like this:也许你想要这样的东西:
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!")
Use用
cc = max(0, cc)
and avoid the if
statement altogether.并完全避免使用
if
语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.