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
Related
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 1 year ago.
Improve this question
def factors(x):
if x >= 0:
for i in range(1,x): # cannot do mod on 0
if (x%i) == 0: # a factor evenly divides with no remainder
print(i, end= " " )
else: print("error message ")
factors(21)
factors(-1)
factors(-3)
How can I print factors more organized so you can tell where each factor came from? for example I wanted to print " Factors for 21 are ...... etc. however they are on the same line
My output is :
1 3 7 error message
error message
and I want my out put to be
Factors for 21 are : 1 ,3 ,7
error message
error message
The solution is about finding the right structure. Since you want "Factors for 21 are :" printed first you should start the function printing that. Since you want a new line, you could insert a simple print() after the for-loop.
A solution could be:
def factors(x):
if x >= 0:
print(f"Factors for {x} are :", end= " ")
for i in range(1,x): # cannot do mod on 0
if (x%i) == 0: # a factor evenly divides with no remainder
print(i, end= " " )
print()
else: print("error message ")
factors(21)
factors(-1)
factors(-3)
Always remember that code works in the order you write. So if you want A to happen before B happens, then write the code for A before the code for B.
Try this:
def factors(x):
if x >= 0:
print(f"Factors for {x} are:", end=" ")
for i in range(1,x): # cannot do mod on 0
if (x%i) == 0: # a factor evenly divides with no remainder
print(i, end=", ")
print()
else:
print("error message")
factors(21)
factors(-1)
factors(-3)
We are first printing out the "Factors for..." and putting the end argument so the result happens all on 1 line. Then, in the for loop, we are iterating and place a comma after each iteration. Then, we are using the a regular print statement to create a new line before you print out "error message".
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 2 years ago.
Improve this question
I'm new to python and I've looked up a little bit of info and i cant't find the problem with my code, please help.
Code:
array = []
print ('Enter values in array: ')
for i in range(0,5):
n = input("value: ")
array.append(n)
a = input("Enter search term: ")
for i in range(len(array)):
found = False
while found == False :
if a == array(i):
found = True
position = i
else :
found = False
print("Your search term is in position " + position)
Error: at if a == array(i) line it says
list object is not callable
While it's generally hard to help without knowing what the error is, it's obvious here:
array(i)
is function call syntax, but lists aren't callable – you'll want subscript syntax:
array[i]
You need not have to run a while loop again while travering the array
array = []
print ('Enter values in array: ')
for i in range(0,5):
n = input("value: ")
array.append(n)
a = input("Enter search term: ")
for i in range(len(array)):
if a == array[i]:
position = i
if position:
print("Your search term is in position " + str(position))
else:
print('Not Found')
Here try this.
Also List/Dict can be accesed as list[i] not list(i)
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}')
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
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'