Hi,
I have 2 if loops to be executed based on input from user, but only 1 loop works at a time even if the condition satisfices, I had to comment out the other loop in order to work.
Is it indentation causing any issues here?
I'm
x = input("Please enter the value for x:")
y = input("Please enter the value for y:")
if(x == 10 and y == 30):
body;
else:
print('I'm sorry, your wrong')
if (x == 40 and y == 50):
body;
else:
print('I'm sorry, you entered x as 70)
There's a number of things:
1st You can't have print('I'm sorry, your wrong')
because Python does not know where the string begins and ends, so either have print("I'm sorry, you're wrong")
or print('I\'m sorry, you\'re wrong')
. I'd go with with the first option, as it's more readable.
2nd What is the body;
supposed to be?
I'll get the the other issues after you've posted back.
There are no loops here. Loops start with for or while. if is a condition statement, not a loop, and is usually referred to as an "if statement". The code you posted doesn't have anything commented out, and it contains syntax errors that prevent running. Could you please post the real code?
Some comments:
Be consistent with indentation and use of whitespace.
body; is not a legitimate python statement. Use pass or a string as a placeholder
x = input("Please enter the value for x:")
y = input("Please enter the value for y:")
if(x == 10 and y == 30):
pass # code placeholder
else:
print('I\'m sorry, your wrong') # One way to use ' in a string
if (x == 40 and y == 50):
"""Code placeholder"""
else:
print("I'm sorry, you entered x as 70") # a different way around using ' in a string
input() returns a str object, so typing 10<enter> makes x the str "10", not the int 10. If it is important that x is an int, you need to convert the str returned by input() to an int.
Indentation could be a problem here. This code looks very similar to the above but gives a different result.
x = input("Please enter the value for x:")
y = input("Please enter the value for y:")
if(x == 10 and y == 30):
pass # code placeholder
else:
print('I\'m sorry, your wrong')
if (x == 40 and y == 50):
"""Code placeholder"""
else:
print('I\'m sorry, you entered x as 70')
In this code the second if statement is evaluated only if the first if statement fails (x != 10 or y != 30). In the top example both if statements are always evaluated. Both examples are valid Python and the correct indentation is the one that gives the desired results. I can't provide any guidance for which is correct, because neither result makes any sense to me. Both examples print "I'm sorry, your wrong" and "I'm sorry, you entered x as 70" if x == 20.
Okay; I'm out of here, as there's no point in two people (both of whom are more than experienced enough to answer this) posting answers, as it's only going to get confusing for you.