一个大一仔觉得很难的python问题:笨办法学python3的习题25

一个大一仔觉得很难的python问题:笨办法学python3的习题25

问题描述:

问题1:我的代码写好了,然后在powershell里面运行失败了,我尝试了好多别人的同款代码,就是****上面同一道题目的代码,但是都没有运行成功,也尝试了一些之前我失败了积累的方法也没有成功。图片内容是powershell里面运行的样子。

img

这是我的代码,按照书里面一个字母一个字母的打出来的。

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ' )
    return words

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Print the first word after popping it off."""
    word = words.pop(0)
    print(word)

def print_last_word(words):
    """Print the last word after popping it off."""
    word = words.pop(-1)
    print(word)

def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)

问题2:不知道是不是连锁反应,我按照书里面的内容,在powershell里面运行python的时候也失败了。

img

以下是接下来要在powershell里面输入的内容

import ex25
sentence = "All good things come to those who wait."
words = ex25.break_words(sentence)
words
sorted_words = ex25.sort_words(words)
sorted_words
ex25.print_first_word(words)
ex25.print_last_word(words)
words
ex25.print_first_word(sorted_words)
ex25.print_last_word(sorted_words)
sorted_words
sorted_words = ex25.sort_sentence(sentence)
sorted_words
ex25.print_first_and_last(sentence)
ex25.print_first_and_last_sorted(sentence)

最后,谢谢老哥不吝赐教,感激不尽!!

你的函数都没有调用啊

为什么要这样写代码 新手前期更需要ide的帮助纠错 下个vscode或者pycharm写

千万别这么学Python,没用的,Python环境都没配置PowerShell怎么运行啊?