I have this code:
num = range(1,33)
num[0]=1
num[1]=2
for i in range(2,32):
num[i]=num[i-1]+num[i-2]
total=0
for i in range(0,32):
print num[i]
if num[i]%2==0:
total=total+num[i]
else:
num[i]=num[i+1]
I want to find the sum of the even numbers in the num
array. Can anyone suggest what I did wrong here ?
Tomerikoo
18.1k16 gold badges45 silver badges60 bronze badges
asked Apr 15, 2013 at 19:32
Indentation is very important in python
if num[i]%2==0:
total=total+num[i]
else:
num[i]=num[i+1]
should be
if num[i]%2==0:
total=total+num[i]
else:
num[i]=num[i+1]
Also, use consistent indentation e.g 4 spaces every where you have to introduce indentation.
answered Apr 15, 2013 at 19:33
karthikrkarthikr
96.9k26 gold badges196 silver badges188 bronze badges
5
Alternatively:
total = sum([i for i in num if i % 2 == 0])
-
Sum( ) will return the summation of a list.
-
[i for i in num if i % 2 == 0]
is a List Comprehensions.
For example:
>> num = [1,2,3,4]
>> tmp = [i for i in num if i % 2 == 0]
>> print tmp
[2,4]
>> total = sum(tmp)
>> print total
6
answered Apr 15, 2013 at 19:37
0 / 0 / 0 Регистрация: 25.12.2018 Сообщений: 10 |
|
1 |
|
28.12.2018, 21:17. Показов 14471. Ответов 5
Помогите разобраться с ошибками print (‘maximum’,end=»») if b <= a >= c: SyntaxError: bad input on line 5
0 |
swillrocker 337 / 126 / 114 Регистрация: 09.04.2011 Сообщений: 246 |
||||
28.12.2018, 21:36 |
2 |
|||
У меня вроде работает все…
Кликните здесь для просмотра всего текста
1
0 |
0 / 0 / 0 Регистрация: 25.12.2018 Сообщений: 10 |
|
28.12.2018, 21:40 [ТС] |
3 |
Тогда я понял, это в интернете компилятор онлайн какой то глючный
0 |
513 / 145 / 27 Регистрация: 18.04.2015 Сообщений: 1,872 Записей в блоге: 15 |
|
28.12.2018, 22:04 |
4 |
makardasdasd, есть, например программа wing 101 — для работы с кодом
1 |
Garry Galler 5407 / 3831 / 1214 Регистрация: 28.10.2013 Сообщений: 9,554 Записей в блоге: 1 |
||||
28.12.2018, 22:51 |
5 |
|||
SyntaxError: bad input on line 5 Компилятор не глючный — он просто под python 2. Нужно ж думать…
0 |
0 / 0 / 0 Регистрация: 25.12.2018 Сообщений: 10 |
|
28.12.2018, 23:05 [ТС] |
6 |
Я понял, теперь буду знать
0 |
How to resolve the syntax error: bad input?
I am trying to print the sum of even numbers, but I am seeing this error called syntax error: bad input why? Here is my code
num = range(1,40)
num[0]=1 num[1]=2
for j in range(2,41):
num[j]=num[j-1]+num[j-2]
total=0
for j in range(0,41):
print num[j]
if num[j]%2==0:
total=total+num[j]
else:
num[j]=num[j+1]
Answers
I think the mistake is in this part
if num[j]%2==0:
total=total+num[j]
else:
num[j]=num[j+1]
In python alignment is very important try to change your code like this
if num[j]%2==0:
total=total+num[j]
else:
num[j]=num[j+1]
Write your answer
Permalink
Cannot retrieve contributors at this time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#———————Syntax Errors ——————# | |
################### Example 1 ####################### | |
#SyntaxError: bad input (‘ | |
#’) | |
#Cause: Forgetting punctuation | |
def say_hello() | |
print «Hello world!» | |
#This is a very common error. Python requires a colon at the | |
#end of function definitions. Add it into the function above | |
#after «say_hello()» and the error will go away. | |
#You might find that your error doesn’t say exactly this. | |
#The «bad input» is whatever text follows the missing | |
#punctuation. If you have a bad input error, no matter | |
#what it says in the parentheses, you should check your | |
#punctuation. | |
May-14-2020, 07:40 AM
(This post was last modified: May-15-2020, 06:24 AM by snippsat.)
def __init__(self, name, gender, hp,attack, mp,exp,level): '''Use double underscores''' #This method initializes the attibutes of the Character class self.name = name self.gender = gender self.hp = hp self.attack = attack self.mp = mp self.exp = exp self.level = level #these are the Character's stats. def __str__(self): return(str(self.name)+": level "+str(self.level)++str(self.hp)+" hp, ") '''To be used in battle''' def level_check(self): if self.level==0: level_exp=50 if self.exp>=level_exp: self.level=self.level+1 level.exp=level.exp*3 stat_points=5 while stat_points>0: #put something here later chara1=Chara(input("What is your name?"),input("Are you a boy or a girl? nt 1. Boy nt 2. Girl"),9,3,0,0,0)
This is my code. On the last line, there is a «SyntaxError: bad input on line 53 in main.py.» I’ve looked through it and found that the problem has to do with my use of the inputs. I can’t find what’s wrong please help.
Posts: 354
Threads: 13
Joined: Mar 2020
Reputation:
9
Don’t post multiple threads, and use proper code tags please.
Can you show the line 53 of your code?
Posts: 3
Threads: 2
Joined: May 2020
Reputation:
0
(May-14-2020, 10:12 AM)pyzyx3qwerty Wrote: Can you show the line 53 of your code?
This is it:
chara1=Chara(input(«What is your name?»),input(«Are you a boy or a girl? nt 1. Boy nt 2. Girl»),9,3,0,0,0)
Also, I did not think that I posted it twice.
Posts: 5,015
Threads: 16
Joined: Feb 2020
Reputation:
279
The error is on line 52 where you don’t have anything inside the while statement. Just add a «pass» command to stub out the code for later
while stat_points>0: pass
Posts: 354
Threads: 13
Joined: Mar 2020
Reputation:
9
(May-14-2020, 03:59 PM)Impropabletank Wrote: Also, I did not think that I posted it twice.
https://python-forum.io/Thread-Bad-input