python-3.x - 程序语法错误

标签 python-3.x pygame syntax-error

我已经开始研究该程序,该程序应该对您播放摇滚剪刀纸。我尝试了不同的模型,这是最接近的模型。唯一的问题是它有一个语法错误,我不知道如何解决。

  #Macchiat0
  #3, 6, 2016

  #3. RPS is played between the computer and a single user.
  #The player is prompted for a throw  when 1 corresponds
  #to Rock, 2 to Paper, and 3 to Scissors.
  #A random number between 1 and 3 is generated for the computer throw.
  #The winner is determined based on the rules of Rock Paper and Scissors.

  #Program Menu

  import random
  end_game = True
  while end_game == True:
  print('1. Enter 1 for Rock ')
  print('2. Enter 2 for Paper ')
  print('3. Enter 3 for Scissors ')
  print('4. Quit')
  ans = int(input('What do you want to do?: '))
  if ans=="1":
        print("\n Enter 1 for Rock: ")
        if Player game = 1
        print('You win')
        if Player game = 2
        print('You loose')
        if Player game = 3
        print('You draw')
if ans =="2":
    print("\n Enter 2 for Paper: ")
        if Player game = 1
        print('You win')
        if Player game = 2
        print('You loose')
        if Player game = 3
        print('You draw')
if ans =="3":
    print("\n Enter 3 for Scissors: ")
        if Player game = 1
        print('You win')
        if Player game = 2
        print('You loose')
        if Player game = 3
        print('You draw')
elif ans=="4":
    print("\n Goodbye")
    break
else:
    print("\n Not Valid Choice Try Again")

最佳答案

  • 如果用2个空格表示,则所有第一行(包括第一个)都包括在内。删除它们。
  • 如果Player game = 1,则为
  • ;如果-子句应以结尾,则所有相似:
  • 什么是玩家游戏?符号玩家游戏= 1毫无意义。您是否应该将答案与某个随机值进行比较?也应该有==而不是=

  • 只是很少注意到。真正的答案是在运行脚本时检查输出。它将告诉您出了什么问题。

    您的问题对SO来说不是最好的,有人会因此而绞死我,但是...该死,这是星期五!干得好:
    import random
    end_game = False
    
    def intToRPS(i):
        if i == 1:
            return 'Rock'
        elif i == 2:
            return 'Paper'
        elif i == 3:
            return 'Scissors'
    
        return ''
    
    while end_game == False:
        print('1. Enter 1 for Rock ')
        print('2. Enter 2 for Paper ')
        print('3. Enter 3 for Scissors ')
        print('4. Quit')
        ans = int(input('What do you want to do?: '))
        comp = random.randint(1, 3)
    
        if ans > 0 and ans < 4:
            msg = 'You chose ' + intToRPS(ans) + ' and computer chose ' + intToRPS(comp)
            print(msg)
    
        if ans == comp:
            print('Draw')
        elif ans == 1 and comp == 2: #Rock vs Paper
            print('You loose')
        elif ans == 1 and comp == 3: #Rock vs Scissors
            print('You win')
        elif ans == 2 and comp == 1: #Paper vs Rock
            print('You win')
        elif ans == 2 and comp == 3: #Paper vs Scissors
            print('You loose')
        elif ans == 3 and comp == 1: #Scissors vs Rock
            print('You loose')
        elif ans == 3 and comp == 2: #Scissors vs Paper
            print('You win')
        elif ans=="4":
            print("\n Goodbye")
            end_game = True
            break
        else:
            print("\n Not Valid Choice Try Again")
    

    关于python-3.x - 程序语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37614436/

    相关文章:

    python - Pygame 迷宫游戏无法正确创建关卡

    python-3.x - 捕获 Aiohttp 中的所有路由

    Python Pandas fillna()

    python - 快速确定 Python 中小于 10 亿的数字是否为质数

    python - 为什么 Pygame 一次只能让一名玩家移动

    python - 基于端点的旋转

    python - 检查列表中的项目未按预期工作

    objective-c - 方法声明中的语法错误 : Expected ')' before name

    PHP MYSQL 错误,查询无效(但不是无效的)

    oracle - SQLPLUS 对象系统。是无效的