2017-07-21 175 views
0
import time 
import random 
print("Welcome to double up!") 
pscore = 1 
pscore = str(pscore) 
print("Your current balance is "+pscore+", would you like to double or 
quit?") 
while pscore != 0: 
    pscore = str(pscore) 
    print("Your current balance is "+pscore+"!") 
    pscore = int(pscore) 
    choice = input("d/q :") 
    if choice == ("d")or("D"): #later simplify cases 
     luck = random.randint(1,100) 
     if luck > 75: 
      print("Upgrade failed!") 
      pscore = 0 
     else: 
      print("Upgrade complete!") 
      pscore = pscore * 2 
    else: #ERROR 
     print("Incorrect command! Please retry!") 

標記爲#ERROR的代碼行不會運行,無論我輸入什麼變量'choice'都有幫助嗎?Python,if-else語句錯誤

我還希望我能做些什麼來幫助清理我的代碼,很新:對

https://pastebin.com/dPbpZfcy - 該代碼爲我搞砸了在這個網站

+1

它絕對不應該運行。您有縮進錯誤。 –

+0

@cᴏʟᴅsᴘᴇᴇᴅ你能告訴我在哪裏嗎? –

+2

'如果選擇==(「d」)或(「D」):#later簡化案例'unindent這一級 – jacoblaw

回答

0

兩個問題:一個是該行

if choice == ("d")or("D"): #later simplify cases 

必須不縮進。第二個問題是if語句返回true,如果choice == ("d")如果("D"),其中第二個總是如此。

0

格式化你的if choice == ("d")or("D"): #later simplify cases線有錯誤identation。

+0

縮進是一個問題,但它不是唯一的問題,而且縮進問題可能甚至不存在於OP的原始代碼中,否則它將不能完全運行,而是執行但不執行'else'。 – Kevin

0

取消縮進這一行:

if choice == ("d")or("D"): #later simplify cases 
+0

不知道你爲什麼被低估,這是正確的。 – jacoblaw

+0

縮進是一個問題,但它不是唯一的問題。而且縮進問題可能甚至不存在於OP的原始代碼中,否則它將不能完全運行,而是執行但不執行'else'。 – Kevin

0
# import time - Unused. you can remove this line 
import random 
print("Welcome to double up!") 
pscore = 1 
pscore = str(pscore) 
print("Your current balance is "+pscore+", would you like to double or quit?") 
while pscore != 0: 
    pscore = str(pscore) 
    print("Your current balance is "+pscore+"!") 
    pscore = int(pscore) 
    choice = input("d/q :") 
    if choice == ("d")or("D"): #later simplify cases # <<< Indent here. 
     luck = random.randint(1,100) 
     if luck > 75: 
      print("Upgrade failed!") 
      pscore = 0 
     else: 
      print("Upgrade complete!") 
      pscore = pscore * 2 
    else: #ERROR 
     print("Incorrect command! Please retry!") 
1

取消縮進if choice ...行一級。然後,更改if語句這樣說:

if choice in ('d', 'D'): 

你以前有什麼結果將總是爲true,並且else永遠不會被執行。