【问题标题】:if input = list[n]: How would I set this up?if input = list[n]: 我该如何设置?
【发布时间】:2019-06-28 19:49:59
【问题描述】:

我总是在线路上收到错误的输入,但我不知道如何解决它。 如果格式不正确,请见谅。

这是在 python 2.6 上

videoGames = ["Fortnite", "Minecraft", "Roblox", "2048", "Mario Cart"]
favGame = raw_input("What is your favorite video game?")
if favGame == videoGames[1]
    print("Hi.")
print("Really?! We both enjoy playing") #add the input that matches with the list

如果用户输入匹配列表中的一项,那么它将打印“真的吗?!我们都喜欢玩(游戏)

【问题讨论】:

    标签: python list if-statement printing


    【解决方案1】:

    您在这一行缺少一个冒号:

    if favGame == videoGames[1]
    

    应该是

    if favGame == videoGames[1]:
    

    要打印游戏:

    if favGame in videoGames:
        print("Really?! We both enjoy playing", favGame)
    

    【讨论】:

    • 非常感谢!它工作了,但现在打印语句没有打印任何东西
      videoGames = ["Fortnite", "Minecraft", "Roblox", "2048", "Mario Cart"]
      favGame = raw_input("What是你最喜欢的电子游戏吗?")
      if videoGames == favGame:
      print("真的吗?!我们都喜欢玩", favGame)
      print("这是我最喜欢的 5 款电子游戏.")
      for n in range (0, len(videoGames)):
      print(str(n + 1) + ". " + videoGames[n])
    • @coolsocks 如我的回答中所述,将if videoGames == favGame: 更改为if favGame in videoGames:
    • @coolsocks 没问题。顺便说一句,欢迎来到 StackOverflow!请考虑为未来的观众接受这个答案。见stackoverflow.com/help/someone-answers
    猜你喜欢
    • 2014-02-14
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多