5 Replies - 2387 Views - Last Post: 02 March 2010 - 10:52 PM Rate Topic: -----

#1 jjr87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-February 10

Charge Account Validation

Posted 24 February 2010 - 02:50 PM

Ok, so I'm currently working on an array-based problem in which there is a list of account numbers (which are given; 18 in total) which you have to put into an array, and the user's input needs to match up with one of those given. If the two numbers match up, a message should display that it is "valid"; if not, then a "invalid" should be displayed. Whether these account numbers can be displayed or not is not specified, but I'd assume that they shouldn't be.

so, my question is:
Do I need to actually type out and store each of these account numbers into an array (if its even possible)?
Here's what I have so far, but it's only yielding "Invalid."


from array import array

valid=[]

#seems like something's wrong here somewhere
for i in range(0,18):
    acct=input("Enter the charge account number: ")
    valid.insert(i,acct)

    if acct[i]==valid:
        out="Account number valid."
    else:
        out="Invalid account."

#don't know why this is here, but these are the given account numbers
valid=[5658845,8080152,1005231,4520125,4562555,6545231,7895122,5552012,3852085,8777541,7825877,7881200,7576641,5050552,8777541,8451277,7825877,1302850,1250255,4851002]
.




If you can gimme some pointers that'd be great. Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Charge Account Validation

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Charge Account Validation

Posted 24 February 2010 - 08:24 PM

I think I misunderstand what you're trying to do, but I'll give it a shot. You have a list of valid values that a user COULD enter. So, we'll need two lists, one for valid values and one for entered values. I think this is what you have in mind:

entered = []
valid = [5658845,8080152,1005231,4520125,
         4562555,6545231,7895122,5552012,
         3852085,8777541,7825877,7881200,
         7576641,5050552,8777541,8451277,
         7825877,1302850,1250255,4851002]

for i in range(0,18):
    acct=int(input("Enter the charge account number: "))
    

    if acct in valid:
        if acct not in entered:
            entered.append(acct)
        out="Account number valid."
    else:
        out="Invalid account."

    print(out)



You'll see some minor changes but most of it is the same. I started by setting up the two lists, one for valid account values, one for the values the user has entered. The second change is that I cast acct to an int since the values in your valid values are all ints, not strings (in python 2.x, this isn't as necessary, but since you failed to specify that you're using 2.x, I assume you're using 3.x). The line
if acct in valid:


is very straightforward, it asks if the value you entered exists in the list of valid values. If it does exist, we should check to see if it's in the entered values list. If it isn't in the entered list, we add it to the end of the list.

Importing the array class was unnecessary, as python's builtin list class is just fine for this job. If it's asking for the user to enter in 18 possible account numbers, then yes, you do have to type them out. There are other ways to implement this kind of thing, such as reading a list of numbers from a text file. I'm also going to assume you're going to do something with this list of entered numbers at the end.

Let me know if this helps or if I missed the mark somewhere.

This post has been edited by atraub: 24 February 2010 - 10:53 PM

Was This Post Helpful? 1
  • +
  • -

#3 jjr87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-February 10

Re: Charge Account Validation

Posted 02 March 2010 - 07:43 PM

Nail on the head. Thanks alot. Looks like I got some reading up to do.
Was This Post Helpful? 0
  • +
  • -

#4 jjr87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-February 10

Re: Charge Account Validation

Posted 02 March 2010 - 08:01 PM

Actually, I have a general question while we're on it..
How did you get it to repeat the user input?
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Charge Account Validation

Posted 02 March 2010 - 09:03 PM

It's called looping. You should definitely read up more on it. You used a loop called a "for" loop. State plainly, loops allow you to repeat a block of code a certain number of times or until a condition has been met.

for i in range(0,18):
    acct=int(input("Enter the charge account number: "))


The first line says:
Do the following 18 times

The second line says:
Ask for user input and then immediately cast it to an integer and assign it to the variable acct

Put these together and you ask the user for input 18 times. As a programmer, loops are one of your best assets, you will be AMAZED at how frequently you can use them.

This post has been edited by atraub: 02 March 2010 - 09:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 jjr87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-February 10

Re: Charge Account Validation

Posted 02 March 2010 - 10:52 PM

Ah, I see. So the number of times it loops depends on the number set in the range; in this case, it'll loop 18 times..Gotcha. Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1