Jump to content
talkfootball365
  • Welcome to talkfootball365!

    The better place to talk football.

Python Programming


football forum

Recommended Posts

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. 
    
  


 

Link to comment
Share on other sites

  • Replies 160
  • Created
  • Last Reply
  • Subscriber
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? 

Link to comment
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. 

Link to comment
Share on other sites

  • Subscriber
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)

 

Link to comment
Share on other sites

  • Subscriber
3 minutes ago, Eco said:

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

Correct try not to think of it as loops more like verification/logic blocks because you're going to use a loop with the next part of the problem (if you want to) the other way is way too tedious imo and that is to type out all the individual elements and their indices and add them up.

Link to comment
Share on other sites

1 minute ago, Mel81x said:

Correct try not to think of it as loops more like verification/logic blocks because you're going to use a loop with the next part of the problem (if you want to) the other way is way too tedious imo and that is to type out all the individual elements and their indices and add them up.

Yeah - so that's what I'm working on now. I assume that I need to implement a range and length function, as the next two problems will require it. 

Link to comment
Share on other sites

1 minute ago, Mel81x said:

Aite I have to call it a night/day haha but good luck @Eco you're getting the hang of it and if you get stuck I'll take a look tomorrow.

Thank you sir - I certainly appreciate your help!

Link to comment
Share on other sites

  • Subscriber

I must say this was a fun exercise... Step 3 in particular was a nice refresher as it got me thinking of how to put it in the simplest way possible! 

This was my final code (putting it under spoiler tag):

A80hwKB.png

Link to comment
Share on other sites

44 minutes ago, nudge said:

I must say this was a fun exercise... Step 3 in particular was a nice refresher as it got me thinking of how to put it in the simplest way possible! 

This was my final code (putting it under spoiler tag):

 

  Hide contents

 

A80hwKB.png

 

 

What are you doing in line 10? 

Link to comment
Share on other sites

  • Subscriber
1 minute ago, Eco said:

What are you doing in line 10? 

Making sure that the code skips the dashes in the credit card number and only takes values of the actual digits in order to avoid errors that would happen if you tried to perform mathematical operations on it.

Link to comment
Share on other sites

8 minutes ago, nudge said:

@Eco not sure if you know this website already or not, but I found http://www.pythontutor.com/visualize.html very helpful as it actually visualises the code and let's you see how it works. Very useful when trying to understand things, in my opinion!

Perfect - thanks. 

This is something I want to add to my list knowledge base, but this exercise has totally irritated me. 

I read you code, and it makes sense, but if I had to replicate it, I'm not sure who well I could do. Absolutely driving me bonkers. 

Link to comment
Share on other sites

  • Subscriber
1 minute ago, Eco said:

Perfect - thanks. 

This is something I want to add to my list knowledge base, but this exercise has totally irritated me. 

I read you code, and it makes sense, but if I had to replicate it, I'm not sure who well I could do. Absolutely driving me bonkers. 

I felt the same way on many occasions when doing my first course, there were quite a few exercices were I literally spent hours on a simple code that could be written in 5 lines only xD Don't get discouraged.

Link to comment
Share on other sites

1 minute ago, nudge said:

I felt the same way on many occasions when doing my first course, there were quite a few exercices were I literally spent hours on a simple code that could be written in 5 lines only xD Don't get discouraged.

I've been working on this particular problem since Friday..including a solid 5 hours today.

But I have been told that it'll get easier, but I first started coding in Python at the beginning on this month. 

Link to comment
Share on other sites

  • Subscriber
15 minutes ago, Eco said:

I've been working on this particular problem since Friday..including a solid 5 hours today.

But I have been told that it'll get easier, but I first started coding in Python at the beginning on this month. 

I think your course also takes the approach "learn it the hard way" xD It's probably for the best, at least for me, finding out how things work on your own with just a little theory beforehand is more efficient than being guided all the way through. You'll be fine!

Link to comment
Share on other sites

1 minute ago, nudge said:

I think your course also takes the approach "learn it the hard way" xD It's probably for the best, at least for me, finding out how things work on your own with just a little theory beforehand is more efficient than being guided all the way through. You'll be fine!

That's good. I have 3 more projects (like the one above) that I have to submit before July 9th to pass the course. 

I think this is definitely teaching me the hard way, it's also an Ivy League school so they tend to make sure the student knows the theory before the process. 

Thank you again! I'm sure I'll be back either tonight or tomorrow with more questions. 

Link to comment
Share on other sites

  • Subscriber
2 minutes ago, Eco said:

That's good. I have 3 more projects (like the one above) that I have to submit before July 9th to pass the course. 

I think this is definitely teaching me the hard way, it's also an Ivy League school so they tend to make sure the student knows the theory before the process. 

Thank you again! I'm sure I'll be back either tonight or tomorrow with more questions. 

I'm sure @Mel81x is your better bet in general as I'm just a beginner myself, haha! But  please do share your exercises here, I'm always in to share the solutions and discuss it, as it's a good chance for me to improve too... 

Link to comment
Share on other sites

Here is the exercise I'm working on now. I'll paste my code and then maybe you and @Mel81x can assist me in explaining why it isn't working. (I feel like this is all I do now. haha)

image.thumb.png.83d370f582c457d55af6762ee1193cf0.png

The code they provide (Below)

image.thumb.png.531851fca8f2e8bc07f102c9bbf0ae5e.png

image.thumb.png.b84722f04f02c402831519e25c97c9ab.png

 

image.thumb.png.e0e33beb7a9cd9d5024c33be48b88e5e.png

 

Link to comment
Share on other sites

Okay - here is what I've done, and to no one's surprise, it's not coming up with any results... :40_rage:

class County :
  def __init__ (self, init_name, init_population, init_voters) :
	self.name = init_name  
	self.population = init_population
	self.voters = init_voters
	self.turnout = init_population // init_voters
		

def highest_turnout(data) :
  highest = data[0]
  highest_turnout = data[0].turnout
  for county in data :
	  if turnout < data :
		  highest_turnout = data.turnout 

  return "highest_turnout"
# your program will be evaluated using these objects 
# it is okay to change/remove these lines but your program
# will be evaluated using these as inputs
allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]  

result = highest_turnout(data) # do not change this line!

I haven't even attempted the tuple portion yet. 

Link to comment
Share on other sites

  • Subscriber
1 hour ago, Eco said:

Okay - here is what I've done, and to no one's surprise, it's not coming up with any results... :40_rage:

 

  Hide contents

 



class County :
  def __init__ (self, init_name, init_population, init_voters) :
	self.name = init_name  
	self.population = init_population
	self.voters = init_voters
	self.turnout = init_population // init_voters
		

def highest_turnout(data) :
  highest = data[0]
  highest_turnout = data[0].turnout
  for county in data :
	  if turnout < data :
		  highest_turnout = data.turnout 

  return "highest_turnout"
# your program will be evaluated using these objects 
# it is okay to change/remove these lines but your program
# will be evaluated using these as inputs
allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]  

result = highest_turnout(data) # do not change this line!

 

I haven't even attempted the tuple portion yet. 

 

So lets talk about what you did here and what can be done to fix it.

lets start at the top of the highest_turnout function

  • highest = data[0] - This is generally how you want to attack this, take the first value and set it to the highest its the best assumption
  • highest_turnout = data[0].turnout - You dont necessarily need to do this because highest is an object so it contains the turnout value already right? 
  • The loop
    • if turnout < data - what is turnout here? there is no quantifier telling the code where this is defined I am guessing this also failed. Ideally you'd do county.turnout < highest.turnout because then you can see what the object values are and then
    • highest_turnout = data.turnout - This will also fail because data is a list and you cant assign all the turnout values so it will fail too.

Take a look at this and see if it makes sense. I only copied my def for highest_turnout

def highest_turnout(data) :
  highest_turnout = data[0] # assign the first object as the highest this contains everything in it
  for county in data :
	  if int(county.turnout) > int(highest_turnout.turnout) : #I match the turnout values using INT which could be made better but its good for demo purposes. In here you can see the county which is the data iterator on the list - data and the highest_turnout object 
		  highest_turnout = county #once i know its the highest I just take the full object and assign it to highest turnout

  return highest_turnout

 

Link to comment
Share on other sites

First off, thank you @Mel81x and @nudge on your help. Please understand that I'm greatly appreciative. 

So here is what I have right now. However, I'm still getting errors messages about ''county' and others being 'undefined'. 

 

class County:
        def __init__(self, init_name, init_population, init_voters):
                name= init_name
                population = init_population
                voters = init_voters
                turnout = init_population // init_voters #this is my own idea, as I'm trying to add the value for the variable that we will use to deteremine the voter turnout. 


  def highest_turnout(data) :
    highest_turnout = data[0].turnout
    for county in data:
            if int(county.turnout) > int(highest_turnout):
                highest_turnout = county


    return highest_turnout #I continue to get errors from this code stating that county and highest_turnout are not defined, however I keep watching videos and I can't understand why they aren't. 

tup1 = (county, highest_turnout)
print (tup1) #My first poor attempt at a tuple. Idealy, this would certainly give the information that Penn is looking for, however the erros messages I'm getting in visual studios is that 'county' is undefined. 

# your program will be evaluated using these objects 
# it is okay to change/remove these lines but your program
# will be evaluated using these as inputs
allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]  

result = highest_turnout(data) # do not change this line!
print(result) # prints the output of the function
# do not remove this line!

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    No registered users viewing this page.


Sign up or subscribe to remove this ad.


×
×
  • Create New...