我最近開始從一本書學習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:在格式化文章代碼的過程中,我只需要縮進尚未縮進的行,所以我認爲在嘗試複製代碼時可能會遇到縮進問題,因爲某些行缺少縮進?無論如何,我希望這不是問題)
你可以寫適當縮進代碼? – Gahan
對不起,我添加了正確的縮進(我認爲)。 – Bill
不,你沒有。您需要爲代碼的每一行添加4個空格以進行正確的縮進。 –