simple python program doesnt run. shows intendent error - python

I'm trying to fix a problem like if a number is odd or even. this is a simple program to find if a number is odd or even
x=input("enter x")
if x%2==0:
print "even"
else
print "odd"
shows indented error in line 3.
plz help

okay, first of all -
you haven't formatted your code properly. Use the shortcut ctrl+K to do so when you're posting a question. I assume this is what you're looking for:
x = input("Enter X")
if x % 2 == 0:
print "Even"
else:
print "Odd"

Related

Loop is not geting exited in Python

I am trying to write a simple progrm in Python but when my number is getting equal to number the else statement keeps repeating itself. Help
print('Tell me your name Ramesh')
import random
name=input()
print('Howdy, '+name)
print('Guess my number which is in between 1 to 20')
number=input()
mynumber = random.randint(1, 20)
count = 0
while(mynumber!=number):
if (int(number) > mynumber):
print('Howdy, Your number is too high. Plz renter')
number=input()
count=count+1
elif (int(number)< mynumber):
print('Howdy, Your number is too low. Plz renter')
number=input()
count=count+1
else :
print('Howdy you got it in '+ str(count)+' chances. The correct number is '+number)
I've only just started python myself and had the same issue.
Your if statements all convert the entered number to an int however your while doesn't. so it's comparing an int and string. Try:
while(mynumber != int(number))
or change the input to read
number = int(input("blah")).
I like this more even though it will throw an error if you enter text.
You may also need to change the while to read while(mynumber != int(number)) as well as adding in the break within the ELSE. You could also remove the else and just let the loop fall out if the numbers match and then show the success message.

My program is not printing out the message I want

I have written a prime number program and I'm having a trouble with printing out the message "Neither prime nor a composite" as below. I think my code is fine as it is. I'd highly appreciate any comment on this issue. Thank you, in advance
def prime_number():
a = input("Please enter a number:")
x = True
s = 0
for i in range(2, a):
while x:
if a%i == 0:
x = False
elif s:
print s,"Neither a prime nor a composite"
else:
x = True
if x:
print a,"is a prime number."
elif s:
print s,"Neither a prime nor a composite"
else:
print a,"is not a prime number."
prime_number()
Your variable s will stay equall to 0 which is equall to False forever, so there is no way that Your code is going to print "Neither a prime nor a composite"
Just like Chantwaffle said, you'll never get s equal to anything else than 0, because there's no code changing it to anything else. Also, if a <= 2 you'll never enter this for loop, and elif s is always False, since s is defined as 0 at the beginning. Rewrite that code thinking of what you want to achieve and what should be done to get it. Repair logic of this code.

If and elif in Python for good programming practices

Hello there I'm currently trying to get a good grasp of the if, elif, else structure in Python. I'm trying some weird combinations in python having a test program to know the output in this if, if, elif, elif, else code. However I'm getting weird results such as this
input = raw_input('Please enter the required digit: ')
intput = int(input)
if intput == 0:
print 'if1'
if intput == 1:
print 'if2'
elif intput == 0:
print 'elif1'
elif intput == 1:
print 'elif2'
else:
print 'else'
if I in put 1 it will print "if2", I thought that it will also print "elif2" and other shenanigans when I try to change the "intput == n" code. So my question is do I have to stick to the if,elif, elif, .... n * elifs, else method which seems to me working alright than working with the wacky if,if.... n * ifs, elif, elif, ...n* elifs, else.
Thanks
The elif tree is designed such that at anywhere along if one of the statement turns out to be True, the rest of the elifs will not be evaluated.
Here's a tutorial that might help you understand if else better.
this may be more clear to understand:
if input == 0:
print "if1"
switch(input):
case 1:
print "if2"
break
case 0:
print "elif1"
break
case 1:
print "elif2"
break
default:
print "else"
break
of course, the code does not work.

query in Conditional Statements in Python

I am newbie to Python and programming environment .Now I am studying python via online .I Landed up a small trouble while studying If Statements . The IF statement is currently interpreting while the ELSE statement is written it displays me a syntax error attached Screen Shot will make my problem understandble more clearly:
not only ELSE statement even ELIF statement also displays a syntax error.
the if and else need to be indented the same amount
for example
x = 5
if 8 > x:
print "8 is greater than x"
else:
same goes for elif
x = 5
if 8 > x:
print "8 is greater than x"
elif 8 < x:
print "8 is less than x"
else:
Indentation is everything in python. By
if x%2==0:
print "even"
else:
^^^
You mean to say that the else statement also comes under the if block, as it is one block of indent after the if statement. This throws an error as there is no if for the else that you have given. Now lets come to the correct way:
if x%2==0:
print "even"
else:
print "odd"
Here, since if and else are in the same indent, the else is corresponding to the if, and the else is executed if the if condition fails.
Check your spacing, else should be under if
if (x%2 == 0):
print "x is even"
else:
print "x is odd"

My Python Equation Solver Can't Solve for Fractions

Ok, so I made a small equation solver that solves for x. It works great for integer values, but whenever I try to solve for fractions it goes into an endless loop. Here is my code:
print "Make the variable in your equation stand for x"
print
startingLimit=int(raw_input("What is the lowest estimate that your variable could possibly be?"))
print
wholeNumber=raw_input("Do you know if your variable will be a whole number or a fraction? Answer: yes/no")
if (wholeNumber== "yes"):
print
fraction= raw_input("Is it a decimal/fraction? Answer:yes/no")
if (fraction=="yes"):
print
print "This program will only calculate up to the 2nd place to the right of the decimal"
xfinder=float(0.01)
else:
xfinder=int(1)
else:
xfinder=float(0.01)
leftEquation=raw_input("Enter your left side of the equation:")
print
rightEquation=raw_input("Enter the right side of the equation:")
print
amountSolutions=100
print
#solving
a=0
indivisualCount=0
count=0
x=float(startingLimit)
while (count!=amountSolutions):
ifstuffleft=eval(leftEquation)
ifstuffright=eval(rightEquation)
if (ifstuffleft!=ifstuffright):
x=float(x+xfinder)
print x
else:
a=(a+1)
count=count+1
print "Solution",a,"=",x
print
x=float(x+xfinder)
if (ifstuffleft!=ifstuffright): should become
if abs(ifstuffleft - ifstuffright) < tolerance:
Where tolerance is the error you're willing to accept

Categories