exercise 11

作者: 不娶名字 | 来源:发表于2017-12-30 20:18 被阅读0次
    print ("How old are you?", end = ' ')
    age = input ()
    print ("How tall are you?", end = " ")
    height = input ()
    print ("How much do you weight?", end = " ")
    weight = input ()
    
    print(f"So, you're {age} old, {height} tall and {weight} heavy.")
    

    练习

    1. Go online and find out what Python’s input does.
    2. Can you find other ways to use it? Try some of the samples you find.
    3. Write another “form” like this to ask some other questions.

    答案

    1. input函数直接读取输入;input函数将输入当做字符串看待,返回字符串类型。

    例子:

    >>> age = input('your age?')
    your age?34
    >>> print(age,type(age))
    34 <class 'str'>
    >>> age = int(input('your age?'))
    your age?34
    >>> print(age,type(age))
    34 <class 'int'>
    

    另外在Python2.x中的input()和python3.x的input()是不同的。
    以下是stackoverflow的一个答案:

    The difference is that raw_input() does not exist in Python 3.x, while input() does. Actually, the old raw_input() has been renamed to input(), and the old input() is gone, but can easily be simulated by using eval(input()). (Remember that eval() is evil, so if try to use safer ways of parsing your input if possible.)

    1. 还可以使用eval(input())

    name = input("what's your name?")
    print("Nice to meet you, {}.".format(name))
    

    相关文章

      网友评论

        本文标题:exercise 11

        本文链接:https://www.haomeiwen.com/subject/ackngxtx.html