如何修复错误:预期缩进块

时间:2014-04-13 00:07:13

标签: python-3.x indentation

我不断收到expected an indent block的错误消息。 shell将粗体点突出显示为错误。有关我如何解决它的任何建议? 我只是一个初学者,所以任何和所有的帮助将不胜感激!提前谢谢!

from random import *

s = choice( [ 'rock', 'paper', 'scissors' ])

def rps( ):
""" runs a game of Rock Paper Scissors between the program and the user by returning the user's choice and the program's choice and signaling a winner
    input: the user's choice which is one of three responses in the game Rock-Paper- Scissors - all are a string of letters
    """

print ('rps( )')
print ('Welcome to a game of Rock Paper Scissors!')
print ('I just made my choice.')
print ('I promise no cheating this time!')
r = input('Now you choose rock, paper, or scissors!')       
print                                           
  

如果r =='paper':

       print ('You chose paper.')
       if s == 'scissors':
              print ('I chose scissors. Haha, I win fair and square!')
       elif s == 'rock':
              print ("I chose rock. Maybe I'll have better luck next time...")
       elif s == 'paper':
              print ('We need a rematch!')

elif r == 'scissors':
       print ('You chose scissors.')
       if s == 'rock':
              print ('I chose rock. Haha, I win fair and square!')
       elif s == 'paper':
              print ("I chose paper. Maybe I'll have better luck next time...")
       elif s == 'scissors':
              print ('We need a rematch!')



elif r =='rock':
       print ('You chose rock.')
       if s == 'paper':
              print ('I chose paper. Haha, I win fair and square!')
       elif s == 'scissors':
              print ("I chose scissors. Maybe I'll have better luck next time...")
       elif s == 'rock':
              print ('We need a rematch!'

else:
       print ("Don't you know the rules? Choose rock, paper or scissors!")

2 个答案:

答案 0 :(得分:1)

我想我修好了!

来自随机导入*

s = choice(['rock','paper','scissors'])

def rps():        “”通过返回用户的选择和程序的选择并发出胜利者信号,在程序和用户之间运行一个Rock Paper Scissors游戏           从技术上讲,此功能没有输入,也没有返回值;该功能是用户和程序之间的交互        “”“

   print ('Welcome to a game of Rock Paper Scissors!')
   print ('I just made my choice.')
   print ('I promise no cheating this time!')
   r = input('Now you choose rock, paper, or scissors!')

   if r == 'paper':
          print ('You chose paper.')
          if s == 'scissors':
                 print ('I chose scissors. Haha, I win fair and square!')
          elif s == 'rock':
                 print ("I chose rock. Maybe I'll have better luck next time...")
          elif s == 'paper':
                 print ('We need a rematch!')

   elif r == 'scissors':
          print ('You chose scissors.')
          if s == 'rock':
                 print ('I chose rock. Haha, I win fair and square!')
          elif s == 'paper':
                 print ("I chose paper. Maybe I'll have better luck next time...")
          elif s == 'scissors':
                 print ('We need a rematch!')



   elif r =='rock':
          print ('You chose rock.')
          if s == 'paper':
                 print ('I chose paper. Haha, I win fair and square!')
          elif s == 'scissors':
                 print ("I chose scissors. Maybe I'll have better luck next time...")
          elif s == 'rock':
                 print ('We need a rematch!')

   else:
          print ("Don't you know the rules? Choose rock, paper or scissors!")

答案 1 :(得分:0)

好的......所有这一切都很困惑。

首先,除了评论之外,函数rps()内部没有任何内容。这是您定义函数的方式:

def hi_im_a_function():
    hi im inside the function

hi, im not inside the function.

hi_im_a_function()

^调用函数

事实上,整个功能似乎已经过时了。我不知道你用print('rps()')这个东西想要完成什么,但它没有做任何事情。我建议你放弃它。

错误来自错误发生前的行上的随机浮动print。错误登陆if r == paper行,因为这是编译器首先感受到您在其前面的行中设置的错误的影响。

和以前一样,我不知道你在那里做了什么,但那句话在那里没有任何意义。删除它。

修复这些错误,它应该像魅力一样。