【问题标题】:How can I ask for user input in Python?如何在 Python 中请求用户输入?
【发布时间】:2022-11-11 20:13:53
【问题描述】:

所以要开始编码,我必须要求用户输入。他们必须像这样放置4张牌

A-S,A-H,A-C,A-D

然后我会根据他们的输入创建一个列表。它应该从他们的输入中获取第二个元素,然后是第四个元素

4cards = input()
List1 = []
List1.append(4cards[1], 4cards[3])
List1pair = ', '.join(P2)
print 'List1 cards: {0}.format(List1pair)'

这应该或者我希望它打印出来(我还没有检查)

List1 cards: A-H,A-D

但我的代码返回语法错误“SyntaxError:无效语法”

4cards = input()

我该如何解决这个问题?

【问题讨论】:

  • Identifiers and keywords - “标识符的有效字符与 Python 2.x 中的相同:大写和小写字母 A 到 Z、下划线 _ 和,除了第一个字符,数字 0 到 9。”
  • 哦,所以我的问题是我错误地命名了一个变量

标签: python python-3.x


【解决方案1】:

您不能使用数字前 4 卡。使用可变 I.N _4 卡

_4cards = input()
List1 = []
List1.append(_4cards[1], _4cards[3])
List1pair = ', '.join(P2)
print ('List1 cards: {0}.format(List1pair)')

【讨论】:

    【解决方案2】:

    嗨,由于变量 4cards 的名称,您收到错误消息,我们不能将其用作变量名称,它以数字开头,因此您可以使用 FourCards , Cards4 e.t.c

    【讨论】:

      猜你喜欢
      • 2017-07-05
      • 2019-06-20
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      相关资源
      最近更新 更多