如何在分割2个整数时找到余数 [英] How can I find the remainder when dividing 2 integers

查看:69
本文介绍了如何在分割2个整数时找到余数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组:

colors = [" #ff0000","#00FF00","#0000FF"]

colorIndex = 0;


我想通过每个颜色元素循环


for str in strings:

打印颜色[ colorIndex ++%colors.length]

但是当我执行脚本时出现无效的语法错误:

打印颜色[colorIndex ++%colors.length]

^

语法错误:语法无效

I have a string array:
colors = ["#ff0000", "#00FF00", "#0000FF"]
colorIndex = 0;

and I want to loop thru each element of colors

for str in strings:
print colors[colorIndex++ % colors.length]
But i get an invalid syntax error when I execute the script:
print colors[colorIndex++ % colors.length]
^
SyntaxError: invalid syntax

推荐答案

si *************** @ gmail.com schrieb:
我有一个字符串数组:
colors = [" #ff0000","#00FF00","#0000FF"]
colorIndex = 0;

我希望循环遍历每个颜色元素

str中的字符串:
打印颜色[colorIndex ++%colors.length]

但是我的语法无效执行脚本时出错:
打印颜色[colorIndex ++%colors.length]
^
SyntaxError:无效语法
I have a string array:
colors = ["#ff0000", "#00FF00", "#0000FF"]
colorIndex = 0;

and I want to loop thru each element of colors

for str in strings:
print colors[colorIndex++ % colors.length]
But i get an invalid syntax error when I execute the script:
print colors[colorIndex++ % colors.length]
^
SyntaxError: invalid syntax



Python没有后递减运算符。也不是预增量。


你必须发表一个声明,比如


colorIndex + = 1


在你的循环中。


Diez


Python doesn''t have the post-decrement-operator. Also not a pre-increment.

You have to put a statement like

colorIndex += 1

in your loop.

Diez


si *************** @ gmail.com 写道:
I有一个字符串数组:
colors = [" #ff0000","#00FF00","#0000FF"]
colorIndex = 0;

我想要循环遍历每个颜色元素

for str in strings:
打印颜色[colorIndex ++%colors.length]

但是当我执行时出现无效的语法错误脚本:
打印颜色[colorIndex ++%colors.length]
^
SyntaxError:语法无效
I have a string array:
colors = ["#ff0000", "#00FF00", "#0000FF"]
colorIndex = 0;

and I want to loop thru each element of colors

for str in strings:
print colors[colorIndex++ % colors.length]
But i get an invalid syntax error when I execute the script:
print colors[colorIndex++ % colors.length]
^
SyntaxError: invalid syntax



语法错误来自于你使用的++运算符不是modulo

运算符。 ++运算符在python中无效。


david lees


The syntax error comes from your use of the ++ operator not the modulo
operator. The ++ operator is not valid in python.

david lees


好的,我试试你的建议,然后重写我的代码是这样的:

colors = [" #ff0000","#00FF00","#0000FF"]

colorIndex = 0


def getText(nodelist):
str为字符串的



打印颜色[colorIndex%colors.length]
colorIndex + = 1


但我收到此错误:

打印颜色[colorIndex%colors.length]

UnboundLocalError:局部变量''colorIndex''在

赋值之前引用

okay, I try you suggestion, and re-write my code like this:
colors = ["#ff0000", "#00FF00", "#0000FF"]
colorIndex = 0

def getText(nodelist):
for str in strings:

print colors[colorIndex % colors.length]
colorIndex += 1

but i get this error:
print colors[colorIndex % colors.length]
UnboundLocalError: local variable ''colorIndex'' referenced before
assignment


这篇关于如何在分割2个整数时找到余数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆