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.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
Posts: 1355
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 5 guests