【问题标题】:Python 3 on Terminal different from shell?终端上的 Python 3 与 shell 不同?
【发布时间】:2016-12-31 17:28:14
【问题描述】:

我在 python 中构建了一个简单的文本游戏来展示给朋友,它在 shell 上运行良好,但在终端中出现错误。在 python 中编译终端和 shell 有区别吗?

在 shell 中,会发生以下情况:

但在终端它给了我这个错误:

这有什么原因吗?

我的代码:

"""""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~IMPORT PACKAGES~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""""

from random import randint
import os
import copy
import math
import time
import pickle
import platform
import subprocess as sp

"""""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~LOAD DATA/NEW GAME~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""""

newData = {"assassin" : {'hp' : 100, 'atk' : 250, 'lvl' : 1, 'arm' : 0, 'pow' : {'name' : 'surprise attack', 'lvl' : 0}, 'xp' : 0},
                             "mage" : {'hp' : 100, 'atk' : 200, 'lvl' : 1, 'arm' : 0, 'pow' : {'name' : 'fire','lvl' : 0}, 'xp' : 0},
                             "knight" : {'hp' : 100, 'atk' : 150, 'lvl' : 1, 'arm' : 10, 'pow' : {'name' : 'dodging','lvl' : 0}, 'xp' : 0},
                             "healer" : {'hp' : 500, 'atk' : 50, 'lvl' : 1, 'arm' : 0, 'pow' : {'name' : 'healing','lvl' : 0}, 'xp' : 0}}


if platform.system()=='Windows':
    file = 'filepath'
elif platform.system()=='Darwin':
    file = 'filepath'

"""Clears screen"""
clear = lambda: os.system('cls' if platform.system()=='Windows' else 'clear')

print('Load data?')

while True:
    answer = input()


    if answer == 'yes':
        load_file = open(file, 'rb')
        characterData = pickle.load(load_file)
        load_file.close()
        break
    elif answer == 'no':
        print('Starting a new game...')
        time.sleep(0.5)
        characterData = copy.deepcopy(newData)
        save_file = open(file, 'wb')
        pickle.dump(characterData, save_file)
        save_file.close()
        break
    else:
        print('That input cannot be deciphered. Please type "yes" or "no".')

【问题讨论】:

    标签: python shell terminal


    【解决方案1】:

    看起来您在 shell 中使用的是 Python 3.x,但在终端中使用的是 Python 2.x。 input() 函数在两个 Python 版本中的行为不同。

    只需确保您在终端中使用的是 Python 3.x。如果您两次都需要 Python 2.x,请将 input() 更改为 raw_input()

    【讨论】:

      猜你喜欢
      • 2019-05-24
      • 1970-01-01
      • 2013-06-26
      • 1970-01-01
      • 2015-08-03
      • 1970-01-01
      • 2020-07-05
      • 2012-11-22
      • 1970-01-01
      相关资源
      最近更新 更多