2014-10-01 101 views
0

我一直有我的Python代碼的問題,一直在四處求助。我聽說有關這個網站的混雜的東西,但我沒有其他地方轉,所以我希望有人在這裏可以告訴我光線,看看他們是否可以找出我的代碼有什麼問題(除了它雜亂無章)。在這裏它是如何在Python中正確使用while循環和if語句?

D=False 

while D==False: 

     input=raw_input("please type 1 for 10p, 2 for 20p, 3 for 50p, 4 for 1 pound") 

     while input not in ['1', '2', '3', '4']: 

     print "That is not a correct coin value" 
     input = raw_input("input: ") 
    else: 
     if input=="1": 
      m=m+10 
     if input=="2": 
      m=m+20 
     if input=="3": 
      m=m+50 
     if input=="4": 
      m=m+100 

    print("your current credit in pennys is:") 
    print(m) 

    D2=raw_input("are you done") 
    if D2=="yes" or "Yes": 
     D=True 
    else: 
     D=False 

我有種遺忘的代碼植入在這裏,但你得到的圖片。它一切正常,直到你問到你是否完成了。出於某種原因,即使你不肯定也會繼續。可以在任何好的程序員那裏告訴我什麼用的了它

+0

這是StackOverflow上。我們不會向初學者發佈死亡威脅。 – inspectorG4dget 2014-10-01 20:06:11

+0

如果D2 =「是」或「是」,則使用if if ==「yes」或D2 ==「Yes」:'** not **':如果D2 ==「yes」,則使用' – 2014-10-01 20:22:56

回答

0

if D2=="yes" or "Yes": 

結果始終爲true,因爲它本質上分析是這樣的:

if (D2=="yes") or "Yes": 

你可能想:

if D2 == "yes" or D2 == "Yes": 

哪個更好寫成:

if D2.lower() == "yes": 

或者:

if D2 in ["yes", "Yes"]: 
+0

'或「是」:'? – 2014-10-01 20:21:02

+0

@PadraicCunningham:哎呀,謝謝,忘了實際進行更正。 – mipadi 2014-10-01 20:39:50