• Sign up free today!

    Join in on the discussion, prediction leagues and competitions today! Sign up takes no longer than 5 minutes.

football forum

Python Programming

Recommended Posts

Is anyone excelled at this? This is something I'm trying to pick up, as it's commonly used in the world of Cyber Security, and so I signed up for a class, and it would be nice to actually talk to some people about it. 

Share this post


Link to post
Share on other sites

I've started learning Python a few months ago. Finished two courses so far (MITx Introduction to Computer Science and Programming Using Python, and Harvardx CS50), and am currently enrolled in the IBM's Python Data Science program + do a lot of reading and practising on my own. Really enjoying it.

Share this post


Link to post
Share on other sites

Hit me up too if you'd like to talk about it. I haven't programmed in Python for a while but once I see code I can get back in the groove.

Share this post


Link to post
Share on other sites
23 minutes ago, nudge said:

I've started learning Python a few months ago. Finished two courses so far (MITx Introduction to Computer Science and Programming Using Python, and Harvardx CS50), and am currently enrolled in the IBM's Python Data Science program + do a lot of reading and practising on my own. Really enjoying it.

Really? I actually was looking into the IBM course. 

I'm doing the Penn Course called, Computation Thinking for Problem Solving', and so far it's good, but some there is a problem that I seem unable to get unstuck from. 

Share this post


Link to post
Share on other sites
17 minutes ago, Mel81x said:

Hit me up too if you'd like to talk about it. I haven't programmed in Python for a while but once I see code I can get back in the groove.

Yeah, I'm trying to learn and pass this project, and while I'm not looking for the answer, I am looking at learn WHY the code is written like it is. 

Share this post


Link to post
Share on other sites
3 minutes ago, Eco said:

Really? I actually was looking into the IBM course. 

I'm doing the Penn Course called, Computation Thinking for Problem Solving', and so far it's good, but some there is a problem that I seem unable to get unstuck from. 

Do share it, I'm sure @Mel81x can help you out with understanding the code while I would enjoy the opportunity to test myself and see if I can use what I learned so far...

Share this post


Link to post
Share on other sites
3 minutes ago, Eco said:

Yeah, I'm trying to learn and pass this project, and while I'm not looking for the answer, I am looking at learn WHY the code is written like it is. 

I think thats the most critical thing about coding. Just finding the answer doesn't have any long-term benefits because understanding syntax and the dos and donts to me are far more important.

Got an example of something you're trying to understand?

  • Upvote 1

Share this post


Link to post
Share on other sites

Here is the problem for you to think over (it's probably simple, but I just started this last week.) 

Quote

Description Credit card companies and banks use built-in security measures when creating the account numbers on credit cards to make sure the card numbers follow certain rules (you didn’t think they were random, did you?). This means that there are only certain valid credit card numbers, and validity can quickly be detected by using an algorithm that may involve adding up parts of the numbers or performing other checks.

In this activity, you will implement a function that determines whether or not a card number is valid, according to some simple algorithms. Note that these algorithms are purely made-up; don't try to use them to create fake credit card numbers! :-)

We will assume that the credit card number is a string consisting of 14 characters and is in the format ####-####-####, including the dashes, where ‘#’ represents a digit between 0-9, so that there are 12 digits overall. We will revisit this assumption in the an optional later activity.

In the space below, implement a function called “verify” that takes a single parameter called “number” and then checks the following rules:

1. The first digit must be a 4.

2. The fourth digit must be one greater than the fifth digit; keep in mind that these are separated by a dash since the format is ####-####-####.

3. The sum of all digits must be evenly divisible by 4.

4. If you treat the first two digits as a two-digit number, and the seventh and eighth digits as a two-digit number, their sum must be 100.

image.thumb.png.b3f8f72bb78a0be9eb740fde3ccb7a12.png

The rules must be checked in this order, and if any of the rules are violated, the function should return the violated rule number, e.g. if the input is “4238-0679-9123”, then the function should return 2, indicating that rule #2 was violated because although rule #1 was satisfied (the first digit is a 4), rule #2 was not, since the fourth digit (which is 8) is not one greater than the fifth (which is 0). If all rules are satisfied, then the function should return True.

Note that the card number is not actually a number, but is a string of characters. In Python, you can generally use a string the same way you would use a list, e.g. accessing individual characters using their 0-based index. Hint: You will need to do this for checking all the rules. However, when you access a character using its 0-based index, Python will treat it as a character/letter and not a number, even if it’s a digit, and you need to be careful about how you use it in mathematical operations. For instance, if you had the characters ‘1’ and ‘2’ and try to add them, Python would concatenate them and use them to form a longer string; in this case, you would get “12”.

However, if you try to subtract, multiply, divide, etc. then Python will give you an error.

To convert a character/letter to a number, use the “int” function, e.g. “x = int(‘1’)” will convert the character/letter ‘1’ to the number 1 so that you can use it in mathematical operations. 

You can test your code by changing the argument to the “verify” function (line 9 of the starter code) and trying different inputs to see if you are correctly detecting violations of the different rules. Click the “Run” button to run your program, which will invoke the “verify” function with the argument you specified, and then print out the output that is returned.

Hint: Here are some inputs you can try! ● "5000-0000-0000": return 1 ● "4000-0000-0000": passes Rule #1, return 2 ● "4007-6000-0000": passes Rules #1-2, return 3 ● "4037-6000-0000": passes Rules #

 

  • Upvote 2

Share this post


Link to post
Share on other sites

Thanks, will check it out after football! 

  • Upvote 1

Share this post


Link to post
Share on other sites

Awesome. A problem that deals with the following topics

  • Index access - Getting values at different points in the string
  • Casting 
  • Logical Jumps using IF statements
  • Inverse Matching Logic - This one is all dependent on the problem at hand but here you'd probably wnt to find out if a condition does not match vs looking for a matching condition.

If it were me and I was learning this language from scratch I'd try learning those concepts first. I'd imagine you were told those in the lesson plan that was presented. 

Share this post


Link to post
Share on other sites
Just now, Mel81x said:

Awesome. A problem that deals with the following topics

  • Index access - Getting values at different points in the string
  • Casting 
  • Logical Jumps using IF statements
  • Inverse Matching Logic - This one is all dependent on the problem at hand but here you'd probably wnt to find out if a condition does not match vs looking for a matching condition.

If it were me and I was learning this language from scratch I'd try learning those concepts first. I'd imagine you were told those in the lesson plan that was presented. 

Kind of, but not really. I have flown through this course, however this problem I'm not even sure where to begin. 

My goal is starting at the top, which is to have the output come back as 'False' if the first number is NOT a 4...but even that is something we didn't really go over. We have done all basic sorting, adding and what not. We haven't dealt with coding so that the output variable has text. 

Share this post


Link to post
Share on other sites

Plus, all coding and exercises have been listed as the following - 

values = [.7, 2, 4, 1, 3]
count = 0 
for value in values:...

Which to me reads pretty basic. However in this exercise we are trying to code feedback based on the users input, so I'm not sure who to code so that the output reads 'FALSE' if a number is entered that does NOT begin with 4. 

Does that make sense? 

Share this post


Link to post
Share on other sites

So, here is the code they give us - 

Quote

 

def verify (number)

 

___return True

input = "5000-0000-0000-0000"
output = verify(input)
print(output)

 

I am working on step one, which is to verify that the first number is in fact a '4', otherwise print an error message saying "Invalid Number'.

Here is what I'm thinking, and why....

Quote

def verify (number)

__for i in verify :(because I have to identify the input. However, it's this line that is an error...I tried to change out 'verify' to 'input' and 'number', but none of them are working. 
______if i[0] != 4 : The idea being that if the first number i[0] is NOT 4, then this gives the message seen below. 
_________print("Invalid Number")

 

___return True

input = "5000-0000-0000-0000"
output = verify(input)
print(output)

However, this is coming back with a few error messages, so I'm guessing that I haven't defined something properly. 

Share this post


Link to post
Share on other sites
1 hour ago, Eco said:

So, here is the code they give us - 

I am working on step one, which is to verify that the first number is in fact a '4', otherwise print an error message saying "Invalid Number'.

Here is what I'm thinking, and why....

Quote

 

def verify (number)

__for i in verify :(because I have to identify the input. However, it's this line that is an error...I tried to change out 'verify' to 'input' and 'number', but none of them are working. 
______if i[0] != 4 : The idea being that if the first number i[0] is NOT 4, then this gives the message seen below. 
_________print("Invalid Number")

 

___return True

input = "5000-0000

 

However, this is coming back with a few error messages, so I'm guessing that I haven't defined something properly. 

You're thinking too complicated here, you don't need iteration for the first step as you just need to check if the first digit is equal to 4 (i.e. you don't want the code to go through and check other digits of the credit card number at this point). A simple if statement you've written below that first line in your code is enough on its own.

Share this post


Link to post
Share on other sites
46 minutes ago, nudge said:

You're thinking too complicated here, you don't need iteration for the first step as you just need to check if the first digit is equal to 4 (i.e. you don't want the code to go through and check other digits of the credit card number at this point). A simple if statement you've written below that first line in your code is enough on its own.

That's what I thought (and tried), but here is the error screen I am getting. It appears that I have to define 'i'. 

image.thumb.png.c29d2b63847c9d3a1b70349477301dcf.png

Share this post


Link to post
Share on other sites

tenor.gif

Share this post


Link to post
Share on other sites
3 minutes ago, Eco said:

That's what I thought (and tried), but here is the error screen I am getting. It appears that I have to define 'i'. 

image.thumb.png.c29d2b63847c9d3a1b70349477301dcf.png

Change i[0] to number[0] because you want to check the first character of the number you just entered. You haven't defined "i" so the linker is unsure what i is. When you define a function you pass a value to it and here its defined as "def verify(number).

  • Upvote 1

Share this post


Link to post
Share on other sites

I think I got it. I just gave 'i' a value. 

image.thumb.png.acb69310c9ae2e12bcba69c560bab011.png

Share this post


Link to post
Share on other sites

Damn  - when I change the input to begin with a 4, it still shows up as an error. 

image.thumb.png.e304cd73508ed7979c2fea545c037fe5.png

Share this post


Link to post
Share on other sites
def verify(number) :
    if number[0] != 4:
        return "Rule 1 has failed: Number does not start with 4"


input = "600"

output = verify(input)
print(output)

 

That's what I wrote

Share this post


Link to post
Share on other sites
Just now, Mel81x said:

def verify(number) :
    if number[0] != 4:
        return "Rule 1 has failed: Number does not start with 4"


input = "600"

output = verify(input)
print(output)

 

That's what I wrote

But if the input is changed to 400, it should read valid number, and that is now not happening. 

Share this post


Link to post
Share on other sites
2 minutes ago, Eco said:

Damn  - when I change the input to begin with a 4, it still shows up as an error. 

image.thumb.png.e304cd73508ed7979c2fea545c037fe5.png

Its because Python think its a string not a number thats where casting comes into play but you can also do this.

def verify(number) :
    if number[0] != "4":
        return "Rule 1 has failed: Number does not start with 4"

    return "Valid Number"
    
input = "400"

output = verify(input)
print(output)

 

Share this post


Link to post
Share on other sites

So how does someone check their work? I had just what you had, but no matter what I type in the input field, my output remains "Rule 1 has Failed":dash3:

Share this post


Link to post
Share on other sites
4 minutes ago, Eco said:

So how does someone check their work? I had just what you had, but no matter what I type in the input field, my output remains "Rule 1 has Failed":dash3:

Hehe. Okay lets go over something basic with what I did and i'll try and explain it.

This - "4" is not the same as 4 without the quotes.

The first is a string, the second is an integer. In your case you went for an iteration-based solution, which is fine but you dont have to here.

Keep in mind that when you see something like this "ECO" that Python recognizes it as 

0 - E
1 - C
2 - O

So to access the first value you do VARIABLENAME[0] where 0 is the index you want to find. All strings start at the 0 index.

Now, the alternative way to use what you wrote is to do this.

def verify(number) :
    if int(number[0]) != 4:
        return "Rule 1 has failed: Number does not start with 4"

    return "Valid Number"
    
input = "6000-0000-0000-0000"

output = verify(input)
print(output)

The big difference here is that I took a string and converted it from a string to INT by doing int(number[0]). Now, I can check against another integer and see if its valid. 

Also, something more important to remember is that if the compiler thinks its right it doesn't necessarily mean the syntax and conversions/matches are correct too. That's on the developer to understand and do correctly. I know its tedious but the more you persist the easier it will become to recognize mistakes and fix them. The compiler will try and catch common errors but with Python its a lot more complicated than that and its not the right time to get into it. Also, as a first exercise this is a bit daunting for someone who doesn't understand the language. Try #2 I am around for about 20 minutes more and I'll help.

  • Upvote 1

Share this post


Link to post
Share on other sites

Okay - that works properly. I tried to do that, as in next step, I'll have to look at two separate integers and compare them. 

  • Upvote 1

Share this post


Link to post
Share on other sites

So what am I doing wrong on step two? the 4th digit must be 1 greater than the 5th digit. 

the code I have written is - 
    
    if int(number[3]) == (int(number[5]) + 1):
      return "Rule 2 has Failed"
    return "Valid Number"
    

And this isn't working. In my mind, I'm getting the code to read each individual number separately, and I'm comparing i3 (4th number), with i5 (5th number after assuming that the 'dash' is a integer slot). 

But again, it's not working. 
    
  


 

Share this post


Link to post
Share on other sites
1 minute ago, Eco said:

So what am I doing wrong on step two? the 4th digit must be 1 greater than the 5th digit. 

the code I have written is - 
    
    if int(number[3]) == (int(number[5]) + 1):
      return "Rule 2 has Failed"
    return "Valid Number"
    

And this isn't working. In my mind, I'm getting the code to read each individual number separately, and I'm comparing i3 (4th number), with i5 (5th number after assuming that the 'dash' is a integer slot). 

But again, it's not working. 
    
  


 

Could you post the number you're testing against? 

Share this post


Link to post
Share on other sites

Is the program not reading past my first number? I feel like that has to be the issue.

image.thumb.png.3d577e116bb8879f68def61055d80db7.png

Few issues here...

I want the result to be a list of each output and the result. I can change the wording, but I read the instructions as if they want is to say..

Rule 1 Pass
Rule 2 Fail

or 

Rule 1 Pass
Rule 2 Pass

and so on. This makes me think that once it passes the first instance, then it stops. 

Share this post


Link to post
Share on other sites
3 minutes ago, Eco said:

Is the program not reading past my first number? I feel like that has to be the issue.

image.thumb.png.3d577e116bb8879f68def61055d80db7.png

You're right.

Line 4 exits the function because it sees a return without an IF and wont process line 6. Also you're logical return on LINE 7 should be moved to line 8 and whats in LINE8 should be moved to LINE7.

Think of return "Valid Number" as the last thing you will return after you've done all your IF checks so you dont need two returns with each IF.

See this as you've got the logic right

def verify(number) :
    if int(number[0]) != 4:
        return "Rule 1 has failed: Number does not start with 4"
    
    if int(number[3]) != (int(number[5]) + 1):
        return "Rule 2 has failed: 3RD number must be 1 greater than 4TH number"

    return "Valid Number"
    
input = "4005-4000-0000-0000"

output = verify(input)
print(output)

 

  • Upvote 1

Share this post


Link to post
Share on other sites

Got it fixed then, guess the best way is to just not close the loops and keep going. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Advertisement