How to handle both integer and string from raw_input?

前端 未结 5 399
一向
一向 2021-01-14 12:56

Still trying to understand python. It is so different than php.

I set the choice to integer, the problem is on my menu I need to use letters as well.

How c

5条回答
  •  难免孤独
    2021-01-14 13:28

    How can i use integer and string together? Why can i not set to string than integer?

    Well, string formatting is a pretty useful thing:

    >>> a_string = "Hi, I'm a string and I'm"
    >>> an_integer = 42
    >>> another_string = "milliseconds old"
    >>> we_are_the_champions = "%s %d %s" % (a_string, an_integer, another_string)
    >>> we_are_the_champions
    "Hi, I'm a string and I'm 42 milliseconds old"
    

    You even can just throw that integer into string:

    >>> champions_are_here = "Hi, I'm a string and I'm even older, I'm %d milliseconds old" % 63
    >>> champions_are_here
    "Hi, I'm a string and I'm even older, I'm 63 milliseconds old"
    

提交回复
热议问题