2013-10-29 82 views
-6

试图编写一个简单的程序,介绍课程Comp Comp in Uni。只需编写一个Python错误,读取数字输入并打印出来,如果它们不是按递增顺序排列的话。Python语法错误与变量

我有我的想法会工作,但它一直给我一个语法错误,我想不通为什么,请注意我有零编程经验

n = input 
counter = input 
nonincreasing = True 
While counter <= n and nonincreasing = True 
get a value for nextnumber 

在行While counter <= n and nonincreasing = True它不断给我counter是一个语法错误。

+0

'while'应该全部小写,并且你不能同时子句中使用赋值运算符。 – falsetru

+0

缩进是错误的,虽然应该很小,但缺少':'和'='应该是== == –

+0

语法错误的stackoverflow是你的解释器/编译器。尝试学习阅读它的输出。 (一个较少的错误版本应该是:''while counter <= n且nonincreasing == True:...'')。 – miku

回答

0

while需要小写。

While counter <= n and nonincreasing = True 
get a value for nextnumber # This line needs to be indented 

像这样:

while counter <= n and nonincreasing == True: 
    get a value for nextnumber # Like so 
+1

你留下了太多其他问题; '='是赋值,仍然是一个语法错误。不要使用'== True',它在这里完全是多余的,并且与其他操作符结合会导致操作符链或优先级问题混淆。 –

+0

@游戏仍然回答不完整:(阅读其他答案... –

+0

@GrijeshChauhan你是对的,但上面的例子甚至没有有效的代码,它的伪代码。 –

4

有在此一吨的错误:将小写

  1. While需求。请记住Python区分大小写。
  2. 您需要使用==进行比较测试,而不是=,它用于变量赋值。但是,你甚至不需要这里的== True;它什么也没做。
  3. 我敢肯定你打算在每个input之后加上()
  4. 在while语句的末尾需要冒号。
  5. 最后一行需要缩进(当然,在这种情况下它也应该是注释)。

这是我想你的意思是:

n = input() 
counter = input() 
nonincreasing = True 
while counter <= n and nonincreasing: 
    # get a value for nextnumber 

不过,我可能有固定这个脚本,但你会不停的按这样的错误,直到你学习Python。下面是一些参考:

http://www.tutorialspoint.com/python/python_overview.htm

http://wiki.python.org/moin/BeginnersGuide/Programmers