【问题标题】:Why does my print statement produce a "SyntaxError: invalid syntax"? [closed]为什么我的打印语句会产生“SyntaxError: invalid syntax”? [关闭]
【发布时间】:2019-03-20 09:51:14
【问题描述】:

如何进行以下工作(在 Python 中)?

import random
def roll():
    input1 = input("Player1, type 'ROLL' to roll.")
    if (input1 == "ROLL"):
        dice1 = random.randint(1,6)
        dice2 = random.randint(1,6)
        print("You rolled a " + dice1() + " and a " + dice2() ".")

    else:
        pass

roll()

我明白了:

  File "main.py", line 9
    print("You rolled a " + dice1() + " and a " + dice2() ".")
                                                            ^
SyntaxError: invalid syntax

我还希望它重复“Player1,输入'ROLL'来滚动”。如果输入不等于 ROLL。

【问题讨论】:

标签: python


【解决方案1】:

您附近的语法无效:

print("You rolled a " + dice1() + " and a " + dice2() ".")

使用string format

替换这个

print("You rolled a " + dice1() + " and a " + dice2() ".")

有了这个

print("You rolled a {} and {}".format(dice1,dice2))

注意:您可以使用while loop 继续使用user-input,除非inputROLL 匹配:

因此

import random

def roll():
    while True:
        input1 = input("Player1, type 'ROLL' to roll.")
        if input1 == "ROLL":
            dice1 = random.randint(1,6)
            dice2 = random.randint(1,6)
            print("You rolled a {} and {}".format(dice1, dice2))
            break  
roll()

输出

Player1, type 'ROLL' to roll.whaaaat?
Player1, type 'ROLL' to roll.okay
Player1, type 'ROLL' to roll.roll
Player1, type 'ROLL' to roll.ROLL
You rolled a 2 and 2

【讨论】:

    【解决方案2】:

    print 中有语法错误。

    两个int 值都需要使用str() 转换为字符串。

    dice1dice2 不是函数,所以我们不能在它们旁边写 ()

    print("You rolled a " + dice1() + " and a " + dice2() ".")
    

    应该是

    print("You rolled a " + str(dice1) + " and a " + str(dice2) + ".")
    

    最好可以使用任意字符串自带的format函数。

    print("You rolled a {dice1} and a {dice2}.\n".format(
            dice1=dice1,
            dice2=dice2
        )
    )
    

    【讨论】:

    • 如何添加intstr
    【解决方案3】:

    整个函数应该是:

    import random
    def roll():
        input1 = input("Player1, type 'ROLL' to roll.")
        if (input1 == "ROLL"):
            dice1 = random.randint(1,6)
            dice2 = random.randint(1,6)
            print("You rolled a %s and a %s." % (dice1,dice2))
    
        else:
            pass
    
    roll()
    

    如果您要显示两个变量,dice1dice2

    用途:

    print("You rolled a %s and a %s." % (dice1,dice2))
    

    代替:

    print("You rolled a " + dice1() + " and a " + dice2() ".")
    

    【讨论】:

      【解决方案4】:

      您在打印语句中忘记了逗号或 +。 您还可以将变量 dice1 dice2 称为函数。

      import random
      
      def roll():
          input1 = input("Player1, type 'ROLL' to roll. ")
          print(input1)
          if input1 == 'ROLL':
              dice1 = random.randint(1,6)
              dice2 = random.randint(1,6)
              print("You rolled a " + str(dice1) + " and a " + str(dice2) + ".")
          else:
              pass
      
      roll()
      

      【讨论】:

        【解决方案5】:

        Python 的 random.randint 返回一个整数,因此您无需调用 dice1dice2 变量。 替换这一行:

        print("You rolled a " + dice1() + " and a " + dice2() ".")
        

        用这个:

        print("You rolled a " + dice1 + " and a " + dice2 ".")
        

        参考 Python 的 random module

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-12-05
          • 2021-09-06
          • 2021-07-27
          • 2019-09-20
          • 1970-01-01
          • 2019-07-29
          • 2022-12-12
          • 1970-01-01
          相关资源
          最近更新 更多