2014-04-24 1 views
-1

코딩하고 싶습니다 : cc가 음수이면 cc는 0이되어야합니다. cc는 변수가됩니다. 어떻게해야합니까? 삽입 된 코드는 내 코드의 한 섹션 일뿐입니다.조건문, 파이썬, pyscripter를 사용하여 somehting과 같게 변수를 변경하는 방법

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!") 
+0

그래서 'cc'가 0보다 큰 경우'dd '값에 어떤 영향을 미치지 않습니까? –

+0

dd가 같으면 dd가 음수이면 dd는 0이되어야합니다 – user3569687

+0

그런 다음 조건을 'elif' 절에 넣으면 안됩니다 ... –

답변

0

그냥 =와 새로운 값에 변수를 할당합니다

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이 0보다 큰 경우 elif을 사용했기 때문에 dd이 확인되지 않습니다.

아마도 당신은 다음과 같이 원하는 :

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!") 
+0

매우 사실이며 변경되었습니다. 도움을 주셔서 대단히 감사합니다. (: 코드 오류를 이해하지 못할 때 매우 짜증이납니다 !!! – user3569687

+0

어떻게하면 되나요? – user3569687

+0

내 업데이트 된 코드를 확인하십시오, 맞습니까? – user3569687

0

사용

cc = max(0, cc) 

및 전부 if 문을 피하십시오.

관련 문제