2017-09-22 60 views
0

我最近開始從一本書學習python(通過Al Sweigart發明自己的電腦遊戲),我試圖使用我學習修改的東西我已經做了一些練習,使它們更符合我的喜好。 無論如何,有一個這樣的腳本,我試圖修改,而修改後的版本以我喜歡它的方式運行時,當我嘗試使用交互式shell運行它時,雙擊腳本圖標以使其運行命令行界面(我希望迄今爲止我正在使用正確的術語,因爲我對編程還不是很熟悉),它不能運行。命令窗口打開並沒有任何反應。我的腳本在shell上運行,但不是在命令提示符下運行

下面是在外殼和命令行上同時運行原始腳本:

import random 
import time 

def displayIntro(): 
    print('''You are in a land full of dragons. In front of you, you see two caves. in one cave, the dragon is friendly and will share his treasure with you. the other dragon is greedy and hungry and will eat you on sight.''') 
    print() 


def chooseCave(): 
    cave='' 

    while cave != '1' and cave!='2': 
     print('Which cave will you go into?(1 or 2)') 
     cave=input() 
    return cave 


def checkCave(chosenCave): 
    print('you approach the cave...') 
    time.sleep(2) 
    print('it\'s dark and spooky') 
    time.sleep(2) 
    print('a large dragon jumps out in front of you! he opens his jaws and...') 
    print() 
    time.sleep(2) 
    friendlyCave=random.randint(1,2) 

    if chosenCave==str(friendlyCave): 
     print('gives you his treasure!') 
    else: 
     print('gobbles you down in 1 bite!') 

playAgain = 'yes' 

while playAgain=='yes' or playAgain=='y': 
    displayIntro() 
    caveNumber=chooseCave() 
    checkCave(caveNumber) 
    print('Do you want to play again?(yes or no)') 
    playAgain=input() 

這是修改後的版本(我想表現爲正在鍵入在旅途中,使它看起來文字更身臨其境:

import random 
import time 


def read(string): 
    i=0 
    while i<len(string): 
     print(string[i],end='') 
     time.sleep(0.05) 
     if string[i]=='.': 
      time.sleep(0.5) 
     i=i+1 
    print('') 


def displayIntro(): 
    intro='''You are in a land full of dragons. In front of you, you see two caves. In one cave, the dragon is friendly and will share his treasure with you. The other dragon is greedy and hungry, and will eat you on sight.''' 
    i=0 
    read(intro) 


def chooseCave(): 
    cave='' 
    i=0 
    question='Which cave will you go into? (1 or 2)' 
    j=0 
    print('') 
    read(question) 
    print('') 

    while cave != '1' and cave != '2' and i<=10: 
     cave = input() 
     i=i+1 
    return cave 


def checkCave(chosenCave): 
    approach='You approach the cave...' 
    j=0 
    read(approach) 
    print() 

    spooky='It\'s dark and spooky...' 
    j=0 
    read(spooky) 

    time.sleep(1) 
    print() 
    print('\nA large dragon jumps out in front of you! He opens his jaw and...') 
    time.sleep(1.5) 

    friendlyCave=random.randint(1,2) 

    if chosenCave == str(friendlyCave): 
     print('Gives you his treasure!') 
    else: 
     print('Gobbles you down in one bite!') 

playAgain='yes' 

while playAgain=='yes' or playAgain== 'y': 
    displayIntro() 
    caveNumber=chooseCave() 
    checkCave(caveNumber) 
    print('Do you want to play again? (yes or no)') 
    playAgain = input() 

我試圖消除「結束=‘’」的一部分從打印(字符串[我],結束=「」)行,它也爲它鍵入1可怕的結果正常運行(儘管!字符每行!)

你認爲這個問題是什麼,我怎樣才能修復它,而不是每行輸入一個字符?

謝謝你的時間! Bill

(Ps:在格式化文章代碼的過程中,我只需要縮進尚未縮進的行,所以我認爲在嘗試複製代碼時可能會遇到縮進問題,因爲某些行缺少縮進?無論如何,我希望這不是問題)

+0

你可以寫適當縮進代碼? – Gahan

+0

對不起,我添加了正確的縮進(我認爲)。 – Bill

+0

不,你沒有。您需要爲代碼的每一行添加4個空格以進行正確的縮進。 –

回答

0

「讀取」函數結束時的print()語句將打印一個新行。

沒有打印()全碼:

import random 
import time 

def read(string): 
    i=0 
    while i<len(string): 
     print(string[i],end='') 
     time.sleep(0.05) 
     if string[i]=='.': 
      time.sleep(0.5) 
     i=i+1 

def displayIntro(): 
    intro='''You are in a land full of dragons. In front of you, you see two caves. In one cave, the dragon is friendly and will share his treasure with you. The other dragon is greedy and hungry, and will eat you on sight.''' 
    i=0 
    read(intro) 
def chooseCave(): 
    cave='' 
    i=0 
    question='Which cave will you go into? (1 or 2)' 
    j=0 
    print('') 
    read(question) 
    print('') 
    while cave != '1' and cave != '2' and i<=10: 
     cave = input() 
     i=i+1 
    return cave 

def checkCave(chosenCave): 
    approach='You approach the cave...' 
    j=0 
    read(approach) 
    print() 
    spooky='It\'s dark and spooky...' 
    j=0 
    read(spooky) 
    time.sleep(1) 
    print() 
    print('\nA large dragon jumps out in front of you! He opens his jaw and...') 
    time.sleep(1.5) 
    friendlyCave=random.randint(1,2) 
    if chosenCave == str(friendlyCave): 
     print('Gives you his treasure!') 
    else: 
     print('Gobbles you down in one bite!') 

playAgain='yes' 
while playAgain=='yes' or playAgain== 'y': 
    displayIntro() 
    caveNumber=chooseCave() 
    checkCave(caveNumber) 
    print('Do you want to play again? (yes or no)') 
    playAgain = input() 
+0

但是我想用讀取功能做的事情是每次打印一個字符,它們之間有一個小的時間間隔,使它看起來像某人輸入文本,而不是立即出現。它正常運行,就像我在嘗試使用python shell運行時一樣喜歡它,但是當我雙擊腳本圖標時並不正常。 – Bill

+0

好吧,明白了。編輯答案。這會在您輸入文字時打印文字。但不知道你的雙擊:) –

0

嗯...我試圖解決您的問題。我按照自己的方式編寫了代碼,但它可以在python shell中使用,但不能在命令提示符窗口中使用。然而,這裏是我的代碼:

import random as rnd 
import time 
import msvcrt 

def read(string): 
    for each in string: 
     print(each, end="") 
     time.sleep(0.05) 

def displayIntro(): 

    intro="You are in a land full of dragons. In front of you, you see two caves. In one cave, the dragon is friendly and will share his treasure with you. The other dragon is greedy and hungry, and will eat you on sight." 

    read(intro) 



def chooseCave(): 
    cave='' 
    print() 

    while cave != '1' and cave!='2': 

     read('Which cave will you go into?(1 or 2): ') 

     cave=input() 



    return cave 


def checkCave(chosenCave): 

    read('you approach the cave...') 
    print() 
    time.sleep(2) 

    read("it's dark and spooky") 
    print() 
    time.sleep(2) 

    read('a large dragon jumps out in front of you! he opens his jaws and...') 
    print() 
    print() 

    time.sleep(2) 



    friendlyCave=rnd.randint(1,2) 



    if chosenCave==str(friendlyCave): 

     read('gives you his treasure!') 

    else: 

     read('gobbles you down in 1 bite!') 

    print() 




playAgain="yes" 
while playAgain=="yes" or playAgain=="y": 
    displayIntro() 
    caveNumber=chooseCave() 
    checkCave(caveNumber) 
    read("Do you want to play again? (yes/y or no/n): ") 
    playAgain=input() 
    playAgain=playAgain.lower() 

print("Press any key to continue...") 
while not msvcrt.kbhit(): 
    time.sleep(0.1) 

我想你應該使用Tkinter的(因爲使用Tkinter的是簡單和容易(至少對我來說))學習GUI(圖形用戶界面)。如果你學習GUI,你將能夠使你的程序更有趣。

1

您需要進口SYS和使用sys.stdout.flush()功能得到你想要的字符流。

讀函數應該是這樣的

import random 
import time 
import sys 


def read(string): 
    i = 0 
    while i < len(string): 
     print(string[i], end='') 
     time.sleep(0.05) 
     if string[i] == '.': 
      time.sleep(0.5) 
     # flush stdout after sleep 
     sys.stdout.flush() 
     i = i + 1 
    print('') 

[... rest of the code ...] 

這是很好的做法(PEP8)有數學符號和有條件的運營商之間的空間,如以下

def chooseCave(): 
    [... code ...] 
    i = 0 
    [... code ...] 
    while cave != '1' and cave != '2' and i <= 10: 
     [... code ...] 

另一個PEP8好的做法是,不能通過79最大線路長度。所以,當你有一個很長的字符串,單程不通過79個字符是做到以下幾點

def displayIntro(): 
    intro = ('You are in a land full of dragons. In front of you, you see two ' 
      'caves. In one cave, the dragon is friendly and will share his ' 
      'treasure with you. The other dragon is greedy and hungry, and ' 
      'will eat you on sight.') 
    read(intro) 
相關問題