2017-03-18 185 views
-1

我一直在試圖讓這個Python代碼工作大約一個小時,但我似乎無法修復它。我前幾天進入了Python,所以如果這很容易,那就是原因。 Python文本冒險遊戲不工作

def firstChoice(): 
    time.sleep(2) 
    print('You come across a path, it splits at the end.') 
    time.sleep(1) 
    choice=input('Which path do you take, the left path (1) or the right path (2)? \n') 
    checkChoice() 

def checkChoice(): 
# correct 
    if choice=='1' or choice=='2': 
     correct_choice=randint(1,2) 
     if choice==correct_choice: 
      correct=True 
    if choice!='1' or choice!='2': 
     print('You decide to not take a path, and you die due to random circumstances.') 
     time.sleep(1) 
     print('Take a path next time, or at least take it correctly.') 
     failScreen() 

I've imported everything necessary (time and random)

EDIT: Here's the whole code.

import random 
import time 

choice=0 

def introDisplay(): 
    print('This is the pre-game story.') 
    time.sleep(1) 
    print('It lasts for 5 lines.') 
    time.sleep(1) 
    print('When you can be arsed, fix this.') 
    time.sleep(1) 
    print('Thanks,') 
    time.sleep(1) 
    print('You, from 18/3/17') 
    print() 
    firstChoice() 

def firstChoice(): 
    time.sleep(2) 
    print('You come across a path, it splits at the end.') 
    time.sleep(1) 
    choice=input('Which path do you take, the left path (1) or the right path (2)? \n') 
    checkChoice(choice) 

def checkChoice(choice): 
    correct=False 
    if choice=='1' or choice=='2': 
     correct_choice=random.randint(1,2) 
     if choice==correct_choice: 
      correct=True 
    if choice!='1' and choice!='2': 
     print('You decide to not take a path, and you die due to random circumstances.') 
     time.sleep(1) 
     print('Take a path next time, or at least take it correctly.') 
     failScreen() 




def failScreen(): 
    restart=True 
    print('You have failed.') 
    print('Do you want to retry?') 
    restart1=input('Y or y = Yes. N or n = No. \n') 
    if restart1=='y' or restart1=='Y': 
     restart=True 
    if restart1=='n' or restart1=='N': 
     restart=False 
    if restart1!='n' or restart!='N' or restart!='y' or restart!='Y': 
     failScreen() 
    if restart==True: 
     introDisplay() 
    if restart==False: 
     exit() 

introDisplay() 
+0

您需要通過選擇作爲參數傳遞給函數'高清checkChoice(選擇):'當你把它叫做'checkChoice(choice)'時。如果 –

+0

不起作用... – MobyCoding

+2

請參閱http://stackoverflow.com/help/mcve關於根據網站規則構建更好(更多可回答)問題的指導。另請參閱http://stackoverflow.com/help/how-to-ask - 請注意,我們關心*問題本身*,而不是您遇到問題時編寫的程序類型(並且我們要求**最短的程序有相同的問題** - 最短的程序可能*不會是文本冒險)。如果你的問題關注於你正在寫的程序,而不是你正在遇到的問題,那麼它就集中在錯誤的地方。 –

回答

0

首先,如果您正在使用python2.7,則需要知道input()(如果通過控制檯爲其提供數字),則此功能強制轉換爲int,那麼你需要檢查值一樣,如果choice == 1或者只是改變輸入raw_input()(這是最好的方法),也if choice!='1' and choice!='2':你需要把else:那麼你避免了很多檢查或意外的值。

如果您正在使用的raw_input python3是新的輸入功能,因此,您無需更改

而且,最後,如果你想使用的raw_input(),或者你使用python3您需要更改隨機函數random.choice('1','2'),因爲你是一個比較INT一個海峽,在這裏你有這個代碼爲python2.7變化:

import random 
import time 

choice=0 

def introDisplay(): 
    print('This is the pre-game story.') 
    time.sleep(1) 
    print('It lasts for 5 lines.') 
    time.sleep(1) 
    print('When you can be arsed, fix this.') 
    time.sleep(1) 
    print('Thanks,') 
    time.sleep(1) 
    print('You, from 18/3/17') 
    print() 
    firstChoice() 

def firstChoice(): 
    time.sleep(2) 
    print('You come across a path, it splits at the end.') 
    time.sleep(1) 
    choice=raw_input('Which path do you take, the left path (1) or the right path (2)? \n') 
    checkChoice(choice) 

def checkChoice(choice): 
    correct=False 
    if choice=='1' or choice=='2': 
     correct_choice=random.choice(['1','2']) 
     if choice==correct_choice: 
      correct=True 
      print('You chose the correct path') 
     else: 
      print('You dont chose the correct path') 
    else: 
     print('You decide to not take a path, and you die due to random circumstances.') 
     time.sleep(1) 
     print('Take a path next time, or at least take it correctly.') 
     failScreen() 




def failScreen(): 
    restart=True 
    print('You have failed.') 
    print('Do you want to retry?') 
    restart1=input('Y or y = Yes. N or n = No. \n') 
    if restart1=='y' or restart1=='Y': 
     restart=True 
    if restart1=='n' or restart1=='N': 
     restart=False 
    if restart1!='n' or restart!='N' or restart!='y' or restart!='Y': 
     failScreen() 
    if restart==True: 
     introDisplay() 
    if restart==False: 
     exit() 

introDisplay() 
+0

所有這些工作,但raw_input造成的錯誤,但改變後,輸入一切工作正常。 – MobyCoding

0

I guess you are always ending up in the failScreen因爲您的第二個if聲明使用!=1 or !=2,這將導致總是true ...將其更改爲and以查看是否有幫助。

還林不知道如果choicecheckChoice功能可見。在Java中,您需要在函數外部聲明變量

相關問題