Home  >  Q&A  >  body text

python - CMD运行一个py小程序,CMD窗口一闪而过,里面有input(),为什么还会这样?

用python写了个小程序,用IDE运行没有任何问题,尝试用CMD运行,CMD窗口一闪而过就没有了。。。
代码如下:

# -*- coding:utf-8 -*-

from __future__ import unicode_literals

import random

def play():
    message=['石头','剪刀','布']
    i=input('石头剪刀布:\n0=石头\n1=剪刀\n2=布\n3=退出\n')
    while int(i)!=3:
        result=random.randint(0,2)
        print '对方出:'+message[result]+',我出:'+message[i]
        j= i-result
        if j==0:
            print '你们还是做好朋友吧!\n'
        elif j==1:
            print '对方赢了,下次努力!\n'
        elif j==2:
            print '就知道我是最棒哒!\n'
        elif j==-1:
            print '棒棒哒,真想给自己个么么哒!\n'
        elif j==-2:
            print '小家伙,别嘚瑟,下次赢你!\n'
        i=input('石头剪刀布:\n0 = 石头\n1 = 剪刀\n2 = 布\n3 = 退出\n ')

    print '虽然咱们不分高下,但是咱们还是可以做好朋友的。'

play()

里面有input,按理说应该等我input东西才对吧。

我似乎找到原因了,我之前在idle里运行没有任何问题,然后就直接双击运行试试,结果一闪而过,于是我在CMD里python caiquanyouxi.py运行,出现错误了,错误如下:

请各位赐教!

PHP中文网PHP中文网2741 days ago674

reply all(7)I'll reply

  • 怪我咯

    怪我咯2017-04-18 09:07:58

    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    
    input('石头剪刀布:\n0 = 石头\n1 = 剪刀\n2 = 布\n3 = 退出\n ')

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:07:58

    First of all, why does this happen even if there is input?
    Answer: Because an error occurred before input() was run.
    What error?
    Answer: An encoding error occurred when reading the file, that is, some characters could not be read.
    Solution:
    1. Run with IDE.
    2. Run with Linux virtual machine. (In the final analysis, it’s still windows gbk’s fault)
    3. Try to understand things like encode, decode, and ignore

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:07:58

    Then don’t run it from the command line first, put it in IDEL and see if there is an error

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:07:58

    Check whether Python2 and Python3 are installed at the same time. There is also an input function in Python2, but its function is completely different from the input in 3. It is possible that your IDE uses python3, so it can run normally, but cmd uses python2, so it does not work properly.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:07:58

    unicode encode error
    Refer to this http://transcoder.baidu.com/from=1086k/bd_page_type=1/ssid=0/uid=0/pu=usm%400%2Csz%401320_1002%2Cta%40iphone_2_6.0_2_6.8 /baiduid=D47B67CD8866F5B4716F9F9BCF84DFD7/w=0_10_python+unicodeencode+error/t=iphone/l=3/tc?ref=www_iphone&lid=16340644776571976913&order=6&fm=alop&tj=www_normal_6_0_10_title&vit= osres&m=8&srd=1&cltj=cloud_title&asres=1&title=python%E4%B8 %AD%E7%9A%84%E7%BC%96%E7%A0%81%E9%97%AE%E9%A2%98%3A%E4%BB%A5ascii%E5%92%8Cunicode%E4%B8 %BA%E4%B8%BB%E7%BA%BF..._%E5%8D%9A%E5%AE%A2%E5%9B%AD&dict=30&sec=13731&di=d4da424635e9d09b&bdenc=1&tch=124.78.243.1059.0.0 &tch=124.232.284.1073.1.836&nsrc=IlPT2AEptyoA_yixCFOxXnANedT62v3IEQGG_ytK1DK6mlrte4viZQRASDfuL7_KZpPPtCPQpxwGx8Sc_7YskNYWgK&eqid=e2c5a1d90c6a400010 00000257885c4d&wd=&clk_info=%7B%22srcid%22%3A%22www_normal%22%2C%22tplname%22%3A%22www_normal%22%2C%22t%22%3A1468554487103%2C% 22xpath%22%3A%22p-a-h3-em2%22%7D

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:07:58

    Add a u in front of all Chinese characters, for example:

    i=raw_input(u'石头剪刀布:\n0=石头\n1=剪刀\n2=布\n3=退出\n')

    python2’sinputraw_input

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:07:58

    Your error message is that Chinese characters cannot be displayed in the cmd command line

    reply
    0
  • Cancelreply