[英]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.