笨方法學Python》中的習題25出錯

2021-03-08 16:49:17 字數 4919 閱讀 5415

1樓:匿名使用者

>>> sorted_words=ex25.sort_sentence(sentence)

traceback (most recent call last):

file "", line 1, in

sorted_words=ex25.sort_sentence(sentence)

file "e:\學習\python exercise\ex25.py", line 23, in sort_sentence

return (sort_works(words))

nameerror: name 'sort_works' is not defined

sorted_words=ex25.sort_sentence(sentence)    # 呼叫ex25.sort_sentence(sentence)

return (sort_works(words))                        # 返回sort_works(words)

# 錯誤原因sort_works函式沒有被定義

2樓:

在 import 的時候不需要加 .py 字尾

笨辦法學python,習題25import出錯

3樓:匿名使用者

兩種處理:

1、切換

來到源ex25.py的路徑下,再import。切換路徑命令:cd f:\pylearning

2、把ex25.py檔案複製到python安裝目錄的lib資料夾下面,在任意位置都可以import

4樓:匿名使用者

在敲入python之前,先復f:\ 回車制,然後再 cd f:\pylearning,再輸入python, 這樣就不會報錯了。

或者你在進入python提示後,import os, 然後輸入os.chdir('f:\\pylearning'), 再輸入import ex25就不會報錯了。

5樓:匿名使用者

當沒有切換到f:/pylearning時,不會從改目錄下查詢。需要切換到該目錄再import

6樓:匿名使用者

這個實際就是一復個制環境變數的問題,你把你寫

bai 的python檔案所

新手求教,笨方法學python的習題35問題

7樓:匿名使用者

#!usr/bin/python

# -*-coding:utf-8-*-

from sys import exit

def gold_room():

print("this room is full of gold.  how much do you take?")

next = input("> ")

if "0" in next or "1" in next:

how_much = int(next)

else:

dead("man, learn to type a number.")

if how_much < 50:

print("nice, you're not greedy, you win!")

exit(0)

else:

dead("you greedy bastard!")

def bear_room():

print ("there is a bear here.")

print ("the bear has a bunch of honey.")

print ("the fat bear is in front of another door.")

print ("how are you going to move the bear?")

bear_moved = false

while true:

next = input("> ")

if next == "take honey":

dead("the bear looks at you then slaps your face off.")

elif next == "taunt bear" and not bear_moved:

print("the bear has moved from the door. you can go through it now.")

bear_moved = true

elif next == "taunt bear" and bear_moved:

dead("the bear gets pissed off and chews your leg off.")

elif next == "open door" and bear_moved:

gold_room()

else:

print("i got no idea what that means.")

def cthulhu_room():

print ("here you see the great evil cthulhu.")

print ("he, it, whatever stares at you and you go insane.")

print ("do you flee for your life or eat your head?")

next = input("> ")

if "flee" in next:

start()

elif "head" in next:

dead("well that was tasty!")

else:

cthulhu_room()

def dead(why):

print(why,"good job")

exit(0)

def start():

print ("you are in a dark room.")

print ("there is a door to your right and left.")

print ("which one do you take?")

next = input("> ")

if next == "left":

bear_room()

elif next == "right":

cthulhu_room()

else:

dead("you stumble around the room until you starve.")

start()

執行結果如下:

you are in a dark room.

there is a door to your right and left.

which one do you take?

> left

there is a bear here.

the bear has a bunch of honey.

the fat bear is in front of another door.

how are you going to move the bear?

> taunt bear

the bear has moved from the door. you can go through it now.

> open door

this room is full of gold.  how much do you take?

> asf

man, learn to type a number. good job!

笨辦法學python 習題15報錯

8樓:你爸夏洛克

c:\users\admin>d:\python3.

6\learnpythonthehardway\第十五課.py d:\python3.

6\learnpythonthehardway\ex15.txt.txt

here's your file 'd:\\python3.6\\learnpythonthehardway\\ex15.txt.txt':

this is stuff i typed into a file

type the filename again:

> d:\python3.6\learnpythonthehardway\ex15.txt.txt

thisis stuff i typed into a file

第一次bai輸du入和第二次輸入都是需要輸zhi入路徑加dao.txt,這樣返回的結專果才是正確屬的。

9樓:l魯莊公

我也遇到了同樣的問題,網上也是搜不到答案,想了好幾種解決方案,終於版搞定了,首先權**是沒問題。主要是點用了argv這個執行指令,在windows 輸入時如果是變數的話看不出來差別的,但是如果輸入的是檔案的話必須得輸入全路徑,和書上那樣輸入的就會報錯。

希望採納,友情提示一下,python練習的檔案最後不要有中文路徑。

10樓:匿名使用者

no such file or directory: 'ex15_sample.txt'

這提示是沒找到檔案,確認一下檔案是否存在,另外看看路徑是不是有問題。

11樓:心月夕

把ex15_sample.txt放到和ex15.py同乙個目錄下就沒問題了

12樓:虹__橋

應該是沒有寫出ex15_sample.txt的存放路徑。如果ex15_sample.txt和習題15.py存放路徑是一致的,那麼改內

成這樣試試:容python d:\python\----python\習題15.py d:\python\----python\ex15_sample.txt

python中的冒號怎麼用,python中函式引數列表中使用的冒號是幹什麼用的

函式,迴圈,bai條件,類du定義等後面都是跟著乙個zhiblock,block需要縮排,dao所以這些語句後版面都有乙個冒號。至於為什麼權這樣子,我們又不是研究理論的。死記硬背就可以了。什麼是乙個block。每個語言裡幾乎都有有。就是一組語句。比如函式體,迴圈體,條件分支,類的定義等等都是乙個bl...

python中v和f的區別,python中的rawinput和input的區別

回車符復 r 換行符 n 水平製表符制 t 垂直制bai表符 v 換頁符 f v的效果du的確如你zhi程式裡所顯示那dao樣。f在有些終端下與你現在的效果不同。比如可能會清屏。如果你直接向列印口print,那麼會真的換頁。python 中的raw input 和input的區別 1.這兩個函式均能...

python新手,在python中類中的self

任何實現了特殊方法 getitem 的類 內建的列表類list 元祖類tuple 集合類set 字串類str都實現了這個特殊方法,因此它們都可以通過索引來訪問物件的成員 的物件都可以通過索引 下標 來訪問這個物件的成員。而self index 只不過是通過索引來訪問當前物件的成員罷了。這是一個例子 ...