sai peri
sai peri

Reputation: 53

Breaking out of while loop with specific input

I am currently trying to take user input and than break out of it when a condition is met (in this case would be 0). I got the loop working when the if statement is set to inp == ''. When an empty string is entered it breaks out. But if I change the criteria to anything other than '', say 0, the code doesn't break out of the loop.

while True:
    inp = input("Would you like to add a student name: ")
    if inp == 0:
        break
    student_name = input("Student name: ")
    student_id = input("Studend ID: ")
    add_student(student_name, student_id)

I have tried casting the 0 as an int but same issue arises...

EDIT: The above code loops without breaking.

FIX: input takes in a string and I was comparing it to an int. I needed to cast my 0 as a string so the types matched.

Upvotes: 2

Views: 2401

Answers (5)

Acccumulation
Acccumulation

Reputation: 3591

Along the lines of "if you give someone a fish, they'll have food a for a day", there's a reason we ask for a Minimal, Complete, Verifiable Example. You titled your question "breaking out of while loop", but that's not really the issue. The break statement isn't being executed, which should lead you to realize that the if condition is evaluating to False, so the Minimal Example would be "Why is inp == 0 evaluating to False?", rather than the non-minimal "Why is this entire while loop not doing what I expected?" Simply paring a problem down to the smallest component can often be enough to solve a problem: if you had looked at value of inp == 0 and seen that it's False, that should lead you to check the value of inp and see that it's '0' rather than 0.

Upvotes: 0

Kevin Mukuna
Kevin Mukuna

Reputation: 159

while True:
    inp = input("Would you like to add a student name: ")
    if len(inp) == 0 or inp =="": #checking the if the the length of the input is equal to 0 or is an empty string 
        break
    student_name = input("Student name: ")
    student_id = input("Studend ID: ")
    add_student = (student_name, student_id)

print ("The file list-{}.csv is created!".format("something"))

let me know if this what you want. you can't use int because it will expect an integer if the length is not 0 and this is because object of type 'int' has no len.

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 99041

input() returns a string and will never be == to 0, which is an int.
You can either cast (aka Type Conversion) inp to an int or the value to match (0) to a string ('0') before comparison, i.e.:

if inp == str(0): # or simply inp == "0"
   ...

casting inp to an int:

if int(inp) == 0:
    ...

Upvotes: 0

Alec
Alec

Reputation: 9575

You need inp to store the integer input, but input() by default stores a string

while True:
    inp = int(input("Would you like to add a student name: "))
    if inp == 0:
        break
    student_name = input("Student name: ")
    student_id = input("Studend ID: ")
    add_student(student_name, student_id)

Although, if you're asking them to indicate something, you should probably use distutils.util.strtobool(), which accepts a variety of inputs such as 0 or n or no to indicate no.

Upvotes: 0

Ursus
Ursus

Reputation: 30071

As you said, input always gives you a string. Two ways

inp = int(input("Would you like to add a student name: "))
if inp == 0:

or

inp = input("Would you like to add a student name: ")
if inp == '0':

Upvotes: 1

Related Questions