Python Forum
Why both loops don't work in python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why both loops don't work in python?
#1
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 Wall

 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)
Reply
#2
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
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.
Reply
#4
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why does Python not use parenthesis to contain loops? davidorlow 3 3,495 Jun-09-2021, 06:33 AM
Last Post: Gribouillis
  Python for loops Kristenl2784 3 49,767 Jun-16-2020, 06:01 PM
Last Post: Yoriz
  For loops in python Boinbo 3 82,040 Apr-18-2020, 01:23 AM
Last Post: buran
  For loops help, using python turtle SemiBeginnerPY 2 3,963 Mar-10-2020, 10:46 AM
Last Post: SemiBeginnerPY

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020