2014-10-01 105 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