If statement doesn't print desired value [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 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'

Related

If / else function doesn't get fully executed, only the else line [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 months ago.
Improve this question
question = input ('ready/pass').upper()
if question == 'ready':
print ('Okay, let's go!)
elif question == 'pass':
print ('Sad to see you go...')
quit()
else:
print ("Sorry, I don't understand ")
question = input('ready/pass').lower()
if question == 'ready':
print ("Okay, let's go!")
elif question == 'pass':
print ('Sad to see you go...')
quit()
else:
print("Sorry, I don't understand ")
You should use str.lower() instead of str.upper().
"reADy / PASs".upper() # => READY / PASS
"reADy / PASs".lower() # => ready / pass
You are changing your input ready or pass to uppercase but in your condition you are checking with lowercase.

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}')

Why doesn't the 'if' function work? 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 6 years ago.
Improve this question
def guessinggame ():
d = input("Enter a random word: ")
e = input("How many letters does the word has?: ")
f = len(d)
if( "e" == len(d)):
print("You are right!")
guessinggame()
if I try to run this code, it will work, but it will skip the "if" function... Help please. thanks!
You are working only with strings and never converting anything to integers, and (more importantly) you are confusing a string with a reference name. For integers, use int() to cast:
e = int(input("How many letters does the word has?: "))
For the strings vs reference names, 'e' is just the letter "e." Omit the quotes and you will then be using a reference e.
if e == len(d):
e needs to be an int.
def guessinggame():
d = input("Enter a random word: ")
e = input("How many letters does the word has?: ")
f = len(d)
if int(e) == len(d):
print("You are right!")
guessinggame()

Python 3.5.1 if else: invalid syntax? [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 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

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

Categories

Resources