习题14:提示和传递

发布时间:2017-11-09 19:03:23编辑:Run阅读(3583)

    练习代码

    # coding: utf-8
    __author__ = 'www.py3study.com'
    from sys import argv
    script, username = argv
    prompt = '>'
    print("Hi {}, I'm the {} script.".format(username, script))
    print("I'd like to ask you a few questions.")
    print("Do you line me {} ?".format(username))
    likes = input(prompt)
    print("Where do you live {} ?".format(username))
    lives = input(prompt)
    print("What kind of computer do you have ?")
    computer = input(prompt)
    print("""Alright, so you said {} about liking me.
    You live in {}. Not sure where that is.
    And you have a {} computer. Nice.
    """.format(likes, lives, computer))

    将用户提示符设置为变量prompt,这样就不需要在每次用到input()时重复输入提示用户的提示符了,如果需要修改提示符,也只是修改一个位置而已。

    应该看到的结果,记住当运行脚本时,需要把你的名字赋值给这个脚本,让argv参数接收到你的名称

    E:\test>python lianxi_14.py Sam
    Hi Sam, I'm the lianxi_14.py script.
    I'd like to ask you a few questions.
    Do you like me Sam ?
    >yes
    Where do you live Sam ?
    >China
    What kind of computer do you have ?
    >Shanghai
    Alright, so you said yes about liking me.
    You live in China. Not sure where that is.
    And you have a Shanghai computer. Nice.


    常见问题

    运行时出现SyntaxError: invalid syntax

    再次说明,你应该使用命令行,而不是python环境去运行脚本

关键字