Jump to content
talkfootball365
  • Welcome to talkfootball365!

    The better place to talk football.

Python Programming


football forums

Recommended Posts

  • Subscriber

@Mel81x what is, in your opinion, the most efficient way to run a certain python script on all files in a specific folder? Should I just call os.listdir and just loop over all files in that directory?

Something like:

import os
for f in os.listdir(path_to_my_directory):
	# my script here

?

Or is there a better way, especially since there might be thousands of files to work on?

 

EDIT:

I solved it using glob module, but please let me know if there's a better way to do it!

import glob
path = input("Enter the Directory: ")
filenames = glob.glob(path + '/*.html')
for filename in filenames:
	# my code here

 

Link to comment
Share on other sites

  • 4 months later...
Sign up to remove this ad.
  • Replies 160
  • Created
  • Last Reply

Sorry to be a pain. Anyone know where's best to find some ideas for Python coding please?

I only ask as I'm in the middle of doing an assignment and need to do some coding for four exercises. While a couple could be fine, I might need to research some bits to have an idea on the code to write for certain functions.

Link to comment
Share on other sites

  • Subscriber
34 minutes ago, Bluebird Hewitt said:

Sorry to be a pain. Anyone know where's best to find some ideas for Python coding please?

I only ask as I'm in the middle of doing an assignment and need to do some coding for four exercises. While a couple could be fine, I might need to research some bits to have an idea on the code to write for certain functions.

Stack Overflow is your best friend! Also, as silly as it sounds, Google. 

Link to comment
Share on other sites

  • 3 weeks later...

@Eco, @nudge, @Mel81x

I'm on the final exercise of my assignment and have a partially working code for what is meant to be a matching pairs game. I have checked online on quite a few sites but am struggling to find anything that not just corresponds to what I'm doing, but also dealing with the fundamentals only. There's some areas I need to focus on but can't seem to get the following to work.

 

1. The While Loops I have cover the first guesses, but do not cover any other guesses. I think it's mainly where I have placed the While Loops as these need to cover 'out of range' and 'invalid' values such as minus figures and over 12.

2. The code works to a point, but there's part of it that should be a function to stop repeats. I think it's around the guesses due to the potential number of guesses that someone could have, but I can't seem to get a function to work correctly.

3. The random number generator works, but not sure if it could be simplified at all.

 

The code at present is as follows. 

 

theNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
matches = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]
correct = ["?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?"]
userNumbers = []
import random

for counter in range(0, 50):
    random1 = random.randint(0, 11) # random lst
    random2 = random.randint(0, 11) # random lst
    temp = matches[random1] # move random1 to temp
    matches[random1] = matches[random2]
    matches[random2] = temp
print(theNumbers)
print(matches)

print("Welcome to the pairs game. There are six pairs to find.")
print("Here is the board")
print("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11")
print("?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?")

print("Please enter your first guess:")
guess1 = int(input())
while guess1 < 0 or guess1 > 11:
    print("That number is not on the board. Please choose a number from the board:")
    guess1 = int(input())
print("Please enter your second guess:")
guess2 = int(input())
while guess2 < 0 or guess2 > 11:
    print("That number is not on the board. Please choose a number from the board:")
    guess2 = int(input())
if matches[guess1] == matches[guess2]:
    print("Well done. That's a match")
    correct[guess1] = "X"
    correct[guess2] = "X"
    print(theNumbers)
    print(correct)
    print("Please enter your first guess:")
    guess1 = int(input())
    print("Please enter your second guess:")
    guess2 = int(input())
else:
    print("That is not a match. Please try again")
    print("Please enter your first guess:")
    guess1 = int(input())
    print("Please enter your second guess:")
    guess2 = int(input())

# if correct like "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X":
#    print("Congratulations. You have got all the pairs.")

 

I admit that it looks like absolute ass (I am a newbie after all :( ) and I'll still get marks for a partial completion, but any other help would be much appreciated.

Link to comment
Share on other sites

12 minutes ago, nudge said:

@Bluebird Hewitt I'll take a look in a few hours once I'm finished with work, if @Mel81x doesn't help you out before that. Can you post the actual assignment, too?

No worries. I'm on my phone but have cropped the exercise as needed.

It is as follows.

1156133342_Screenshot_20220424-1615502.thumb.png.864e9ebbac9800d0c57f422d97b55051.png

1238093331_Screenshot_20220424-1616012.thumb.png.0a67843d8a8e888f5377c867c78a92f4.png

I'm mainly trying to get it functioning correctly and with validations in place at present (no using minus figures or going 12 and above when choosing a number). The names of each number and the number of turns aren't so important just yet.

 

Link to comment
Share on other sites

  • Subscriber
On 24/04/2022 at 20:56, Bluebird Hewitt said:

No worries. I'm on my phone but have cropped the exercise as needed.

It is as follows.

1156133342_Screenshot_20220424-1615502.thumb.png.864e9ebbac9800d0c57f422d97b55051.png

1238093331_Screenshot_20220424-1616012.thumb.png.0a67843d8a8e888f5377c867c78a92f4.png

I'm mainly trying to get it functioning correctly and with validations in place at present (no using minus figures or going 12 and above when choosing a number). The names of each number and the number of turns aren't so important just yet.

 

Sorry it took me a while to find time... Hope it's still relevant.

So I modified your code by defining a function (attempt) and then running it for a certain number of allowed guesses, and stopping it if all the pairs are found OR once the player runs out of allowed number of guesses. You can definitely remove that and let the game run indefinitely until all matches are found though :) I tested the code under multiple scenarios and it seems to be running without any issues, but it was a quick job so I might have missed something; make sure you try it out extensively!

I didn't look at the random numbers generator part and let it as it is.

theNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
matches = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]
correct = ["?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?"]
userNumbers = []
import random

for counter in range(0, 50):
    random1 = random.randint(0, 11) # random lst
    random2 = random.randint(0, 11) # random lst
    temp = matches[random1] # move random1 to temp
    matches[random1] = matches[random2]
    matches[random2] = temp
print(theNumbers)
print(matches)

print("Welcome to the pairs game. There are six pairs to find.")
print("Here is the board")
print("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11")
print("?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?")

attempts = 0

def attempt():
    print("Please enter your first guess:")
    guess1 = int(input())
    while guess1 < 0 or guess1 > 11:
        print("That number is not on the board. Please choose a number from the board:")
        guess1 = int(input())
    print("Please enter your second guess:")
    guess2 = int(input())
    while guess2 < 0 or guess2 > 11:
        print("That number is not on the board. Please choose a number from the board:")
        guess2 = int(input())
    if matches[guess1] == matches[guess2]:
        print("Well done. That's a match")
        correct[guess1] = "X"
        correct[guess2] = "X"
        print(theNumbers)
        print(correct)
    else:
        print("That is not a match. Please try again") 
    global attempts
    attempts += 1

while attempts < 10: #can change to as many guesses as you want to allow the player
    attempt()
    if correct == ["X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"]:
        print("Congratulations. You have got all the pairs.")
        break
else:
    print("Game over! You ran out of guesses...")

 

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...