While Loop

This is the place for queries that don't fit in any of the other categories.

While Loop

Postby leosava » Tue Oct 29, 2013 4:34 pm

Hey guys I really appreciate the quickly help and feedback, as I mention before I am new here and I am just messing around to see how it works. I am wondering how to stop the loop using while. Probably I am doing some silly mistake.

Code: Select all
xString=input("Enter a value between 1-20: ")
while x>20:
    xString=input("Enter a value between 1-20: ")

yString=input("Enter a value for y between 1-30 : ")
y=int (yString)
print("The sum of", x, "+" , y ,"=", x+y)

Thank you very much
Last edited by micseydel on Tue Oct 29, 2013 4:37 pm, edited 1 time in total.
Reason: Code tags, lock.
Posts: 4
Joined: Sun Oct 27, 2013 9:29 am

Re: While Loop

Postby micseydel » Tue Oct 29, 2013 4:40 pm

Please use code tags in the future. They are essential here, and most of the time (and still important the rest of the time).

Your program says, "while x is less than 20, assign to the variable xString the user input". Since what you're doing inside the loop doesn't change the exit condition, it doesn't ever exit. All the code after your single indented line follows the loop, rather than being inside of it. If you want the code to be inside of the loop, indent it to the same level as the line that is in the loop.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 7 guests