[英]how to change a variable to equal somehting else using conditional statements, python, pyscripter
我想编码:如果 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!")
现在正确了吗???
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!")
只需使用=
将变量分配给新值:
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
>>>
但是,您的代码看起来也有逻辑错误。 如果cc
大于零,则永远不会检查dd
因为您使用了elif
。
也许你想要这样的东西:
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)
并完全避免使用if
语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.