【问题标题】:code won't show in bash (python) [closed]代码不会在bash(python)中显示[关闭]
【发布时间】:2013-04-09 02:07:40
【问题描述】:

我在我的 mac 上使用 python 并通过 wrangler 使用 python,很多东西随机出错,所以我认为我做错了什么。我有一个非常简单的程序,当我在终端上以 bash 运行它时,它不会出现。这是代码:

def shoes():
    j= "jordans"
    a= "adidas"
    n= "nikes"
    question = input("whats ur fav. shoe?")
if a:
    return a

我在 bash 中这样访问它:

cd ~/Desktop/scripts

ls

python shoe.py

然后什么都没有显示

【问题讨论】:

    标签: python bash terminal textwrangler


    【解决方案1】:

    添加这个:

    if __name__=='__main__':
        shoes()
    

    如果你想展示一些东西,请使用print 123sys.stdout(123)

    【讨论】:

    • 所有信息都非常有用,但我仍然没有看到终端中显示的信息,它只是空白。
    【解决方案2】:

    你在那里做什么:

    def shoes():
        j= "jordans"
        a= "adidas"
        n= "nikes"
        question = input("whats ur fav. shoe?")
        if a:
            return a
    

    只是命名-定义-发明了一个函数,该函数在脚本中的其他任何位置都调用(之后)为 shoes()

    当你想调用它时,你应该在某处显式调用它:shoes()

    如果您希望您的 python 脚本定义一个函数,然后使用它,您应该编写:

    # comments start of your script
    
    #there I am defining the function shoes()
    def shoes():
        j= "jordans"
        a= "adidas"
        n= "nikes"
        question = input("whats ur fav. shoe?")
        if a:
            return a
    
    #there I'm calling right now the function shoes()
    shoes()   # there I do it inside the main script
    

    你好像是新手,只是函数的另一个例子:

    def unotherfunction(i):
        for i in range(0,i+1):
            shoes()
    
    #If I call unotherfunction(), then, only then, shoes will be called twice, or fourth, or 7th or depending on the number your set to i like 2, 4 or 7..
    
    unotherfunction(5) # will call 5 times your function shoes() ( asking you 5 times the same question of course ;-)
    

    OPtionnally,也许你也应该添加一个测试,如:

    question = input("whats ur fav. shoe?")
    if a = question:
        return a
    

    这将使输入更有意义。否则,您通过键盘输入的内容根本不属于您的测试。

    然后再次警告,你只有一个返回一个像样值的路径a...注意...你会看到...在其他路径中它会默默地返回None并给你惊喜...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 2013-02-19
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      相关资源
      最近更新 更多