Solved

Python function with while loop and raw_input not breaking

Posted on 2014-04-15
1
670 Views
Last Modified: 2014-04-15
Hello there!

I am preparing a small function the gets feed with csv data in a list and db data in other list.

Those lists look like (for a single record):

['Name', 'Surname', 'Other']

I would like to allow the user to compare two values and decide which value to keep.

My while loop goes on forever even if I have break in it...

What am I doing wrong?

def check_fields(fields_csv, fields_db):
        for field_csv in fields_csv:
            for field_db in fields_db:
                if field_csv == field_db:
                    while True:
                        print ""
                        print "1) Dato nel file csv: " + field_csv
                        print "2) Dato nel database: " + field_db
                        print ""
                        answer = str(raw_input("Which field you want to keep? [1 or 2]: "))
                        print ""
                        if answer == "1":
                            print "You choose 1"
                            global field_import
                            field_import = field_csv
                            break
                        elif answer == 2:
                            print "You choose"
                            global field_import
                            field_import = field_db
                            break

Open in new window

0
Comment
Question by:ltpitt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 40001910
elif answer == "2" ?

answer is a string, so it's should not be equal to a number...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question