2013-10-02 139 views
0

有人可以幫助我,我是編程新手,無法弄清楚爲什麼不能運行?Python令人困惑,需要幫助

import time 
import os 


lives = 3 

def start(): 
    global lives 

    if lives < 1: 
     print("GAME OVER") 
     quit() 

    print("You wake up in a well.") 
    print("Do you climb out or get help?") 
    well = input("Help or Climb? ") 

    if well == "H" or "h" or "help" or "Help": 
     print("You get your phone out.") 
     time.sleep(2) 
     print("There's no charge left.") 
     print(" " * 3) 
     lives = lives - 1 
     print("You have", lives, "lives left.") 
     time.sleep(3) 
     os.system("cls") 
     start() 

    elif well == "C" or "c" or "climb" or "Climb": 
     print("You start climbing up the walls of the well.") 
     time.sleep(3) 
     print("You are close, but you lose your footing and are blinded by the sunlight,") 
     footing = input("Move your foot to the left or right?") 
     if footing == "L" or "l" or "left" or "Left": 
      print("You miss the step and fall.") 
      lives = lives - 1 
      start() 




start() 
+6

當您嘗試運行它時,它會告訴您什麼? – mgilson

+0

'well ==「H」或「h」或「help」或「Help」是錯誤的。 –

+1

這樣的東西:'如果well ==「H」或「h」或「help」或「Help」:'不會起作用,那麼您需要'如果在[「H」,「h」 ,「help」,「Help」]:'或'如果well.lower()[0] =='h':'或類似。 –

回答

0

幾個提示。我建議使用raw_input而不是輸入,而我通常會轉換爲upper,並且只是取第一個字母,例如x = raw_input('> ').upper()[0]以便以後評估更容易:

import time 
import os 


lives = 3 

def start(): 
    global lives 

    if lives < 1: 
     print("GAME OVER") 
     quit() 

    print("You wake up in a well.") 
    print("Do you climb out or get help?") 
    well = raw_input("Help or Climb? ").upper()[0] 

    if well == "H": 
     print("You get your phone out.") 
     time.sleep(2) 
     print("There's no charge left.") 
     print(" " * 3) 
     lives = lives - 1 
     print("You have", lives, "lives left.") 
     time.sleep(3) 
     os.system("cls") 
     start() 

    elif well == "C": 
     print("You start climbing up the walls of the well.") 
     time.sleep(3) 
     print("You are close, but you lose your footing and are blinded by the sunlight,") 
     footing = raw_input("Move your foot to the left or right?").upper()[0] 
     if footing == "L": 
      print("You miss the step and fall.") 
      lives = lives - 1 
      start() 




start() 
-1

在Python 3中正常工作,但在Python 2.7中不能正常工作。原因是well = input("Help or Climb? ")使用input這是Python 3代碼。在Python 2.7中,你想用raw_input代替

+0

該程序包含語義錯誤。 –