listofNumbers = [1, 2, 3, 4, 5, 6]
for number in listofNumbers:
print (number),
if (number % 2 ==0):
print (" is even")
else :
print(" is odd")
print("All done.")
我希望我的代码打印1,2,3,4,5,6并具体打印每个数字是偶数还是奇数,但它只带数字,不带备注,
答案 0 :(得分:0)
您可以删除第一个打印语句-在L3上。现在,它只是循环地通过阵列打印。删除第3行或将其注释掉,在print语句中将数字转换为str类型,它将按您的意愿进行。
listofNumbers = [1, 2, 3, 4, 5, 6]
for number in listofNumbers:
if (number % 2 == 0):
print (str(number) + " is even")
else:
print(str(number) + " is odd")
print("All done.")
答案 1 :(得分:-1)
if / else必须位于for循环内:
listofNumbers = [1, 2, 3, 4, 5, 6]
for number in listofNumbers:
print(number, end="" )
if (number % 2 ==0):
print(" is even")
else :
print(" is odd")
print("All done.")