Python 3.5.1 if else: invalid syntax? [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
number = [1,3,5]
position = 0
while
position < len(number):
numbers = number[position]
if numbers % 2==0:
print('found even number',numbers)
break
position = position +1
else:
I got SyntaxError: invalid syntax, after i push enter after else:
Help me please

The indentation of your position = position + 1 statement is wrong.
It is at the same level as your if and else statements, so separates them.
Indent or move it and you'll be fine.
Also, you could simplify the code a bit by changing the while loop to a for loop as:
number = [1, 3, 5]
for num in number:
if num % 2 == 0:
print("Found even number", num)
else:
Hard to say exactly given the short snippet of code, but it looks like the while loop is unnecessary given what is there

Related

My list python code is producing a sum other than what is intended. Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 days ago.
Improve this question
I am trying to code a program that take user input of orders and converts it to a list that I can then produce a sum based on the number of each word in the list. However, my code keeps producing a a sum that is not correct for the provided list. For example, if I enter Water Nachos Water Cheeseburger the intended sum is 24, but my code is producing 39 as the answer. Why is this and what is a potential fix?
x = input("What are your orders?")
orders = list(x.split())
sum = 0
for i in orders:
if i == "Nachos":
sum+=6
if i == "Pizza":
sum+=6
if i == "Water":
sum+=4
if i == "Cheeseburger":
sum+=10
else:
sum+=5
print(sum)
I expected a sum of 24, but got a sum of 39.
The last else part is only for the if condition i == "Cheeseburger"
So `sum+=5 will gets executed for all the other conditions.
You can just use if else if
x = input("What are your orders?")
orders = list(x.split())
sum = 0
for i in orders:
if i == "Nachos":
sum+=6
elif i == "Pizza":
sum+=6
elif i == "Water":
sum+=4
elif i == "Cheeseburger":
sum+=10
else:
sum+=5
print(sum)

No output when trying to print [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to write a python code in which I input a number, which should be a multiple of 3, and calculate the sum of each digit's power of 3,
ig: input'12' => multiple of 3
should return 1^3 +2^3 = 9
I tried this script and didn't get anything when I run it, what's the problem
number= input('give a number: ')
TT=list(number)
if int(number)==0:
print('wrong number')
elif int(number)%3:
for x in (range(len(TT))-1):
aggregate=sum(int(TT[x])**3)
print(f'the aggregate of {number} is {aggregate}')
None of the conditions are true, so no value ever gets displayed. If you input 12, then 12 % 3 equals 0 (False).
Take a look at the python truth value testing
You should update this line:
elif int(number)%3 == 0:
Now, the whole code needs a revision to get the result that you want. Let me make some changes:
number= input('give a number: ')
aggregate = 0
if int(number)==0:
print('wrong number')
elif int(number) % 3 == 0:
for x in number:
aggregate += int(x) ** 3
print(f'the aggregate of {number} is {aggregate}')

If statement doesn't print desired value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
It should print out stuff stored in generator variable if raw input was 1, but its not doing that, its executing else statement even if I write 1.
from random import randint
print('1. Generate again.')
print('2. Close')
x = raw_input('Pick your selection 1 or 2: ')
if x == 1:
generator = (randint(1000000000000000,999000000000000009))
print generator
else:
print 'bye'
You're comparing a string (what was input) with an int. Try if x == '1' instead.
Turn's answers is a good one, but there is an alternative way to do this by using int(). The built-in function int() treats the string as an int.
from random import randint
print('1. Generate again.')
print('2. Close')
x = raw_input('Pick your selection 1 or 2: ')
if int(x) == 1:
generator = (randint(1000000000000000,999000000000000009))
print generator
else:
print 'bye'

when i put 3 if statements prints all of them [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The following uses variables I have not shown the initialization of, but you can make up your own. It does exactly what I want it to do but it prints all of the if's not just 1 one the if's.
m = input()
if m == 1:
print(l)
time.sleep(1)
else:
print(cost + f + baws)
boss_he = boss_he - 20
print("%" + str(boss_he) + " boss health left")
if m == 2:
time.sleep(1)
else:
print(cost + baws)
boss_he = boss_he - 25
print('%' + str(boss_he) + ' boss health left')
if m == 3:
time.sleep(1)
else:
print(h)
wiz_he = wiz_he + 15
print('%' + str(wiz_he) + " of you health left")
Whenever I run this code it will execute every else case, why?
If each else prints a 1, or a 1, or a 3 if I put that number, it will print 1 2 and 3 instead if just printing 1(if i asked it to print 1). Is it something wrong with my code?
Your input() will never evaluate to any of your if statmenet,s because you are taking input() as a str. Thus, all the elses are triggered. Instead, cast int: m = int(input()). Also, your logic is flawed, because if m is not 1, 2, or 3, all the else statements will be entered.
Why don't you use if,elif,elif and else instead of using if several times? And the program determines your input as string so that make it like m=int(input()) to convert your input to an integer

Python 3.4 - Struggling with a basic program that outputs x if a number from 0-9 is typed [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This is just example code for a larger program I'm making, but I can't seem to get it to work. Basically, Python recognises the <= as incorrect syntax, but I'm failing to see how. What is the correct way to write this line on the program?
number1 = int(input())
if number1 >= 0 and <= 9:
print("x")
else:
print("y")
Any help is appreciated.
You can do as follows:
if 0 <= number1 <= 9:
print("x")
else:
print("y")
Make each logic test separately:
if number1 >= 0 and number <= 9:
or (better) Python supports mathematical-style inequalities:
if 0 <= number1 <= 9:

Categories

Resources