
Dave P. answered 03/31/20
Get your bearings in Python with approachable Stanford CS alumni
Nathan's answer is excellent. In case you are looking for further elaboration, I'm offering the following.
There are three challenges that are especially obnoxious for beginners. Challenge 1) You have to write working code in Python. Best case scenario, you've done this before. Challenge 2) You need to request keyboard input (aka text) from a human user and instruct the Python program to use that text as a number (like 6) which, tragically, the Python program won't figure out on its own. Challenge 3) You will need your program to create a text file and write to it, which is more complicated than it sounds.
Let's start with the most basic element. Does Python work on your machine? Create a file called golfscores.py, with one line of code:
print("Hello, president, I am happy to serve you.")
Run that program. Did it work? If not, you'll have to remember a time when a Python program did run and emulate that. Then, let's do the following check real quick.
import sys
print(sys.version_info[0])
Run this. If you get a 3, ignore this paragraph. If you get a 2, that means you're running an outdated version of Python and life will be extra challenging until you figure out how to upgrade to Python 3, which can be a challenge depending on your development environment.
More on Python2 v Python3 https://www.guru99.com/python-2-vs-python-3.html
You can now delete the code for checking the version.
Now for the keyboard input. We need to ask the user to type in a number of tournaments she/he wants to record, using a prompt, and then we convert their request to an int (a whole number integer). Python needs explicit awareness that the keyboard input should be understood as an integer number instead of the text symbol "3." By default your keyboard input will give you a string, a string is a sequence of text symbols. By default Python will treat the "3" no different than the letter "a", that is to say, as text. You can't count to a symbol like "a," nor a symbol like "3". But the code below will make sure your symbol "3" turns into the countable number 3.
num_tournaments = input("How many weekends worth of tournaments are we trying to record? ")
num_tournaments = int(num_tournaments)
More info on the input function here: https://www.w3schools.com/python/ref_func_input.asp
More info on the int function here: https://www.w3resource.com/python/built-in-function/int.php /
OK, now we're going onto our next challenge: opening a text file.
If you were coding in C++ or Java, opening and writing to a text file would be a week's worth of lessons. We are comparably lucky working in Python. The architects of Python made a not-obvious but yes-convenient utility called the with statement that lets a developer open or create text file, write to it, and close it, without the bonus challenges you can anticipate in other programming languages.
with open("golf.txt", 'w') as silly_file:
#TODO... indented lines of code to work with the file
This with statement does a lot. It gives us a variable I name silly_file, though you can name the variable whatever you please. The variable gives Python magic access to an actual text file we are creating on your computer. The file is called "golf.txt" (or whatever you specify), and file will appear on your computer in the same folder where your golfscores.py file is located. I hope you know where your golfscores.py files is located. If you don't, go find it in your file browser.
I find this to be the clearest tutorial on the with syntax: https://www.pythonforbeginners.com/files/with-statement-in-python
The 'w' part of the open function means 'write' because we're writing a file, not reading one. Learn more about the open options here: https://www.tutorialspoint.com/python/python_files_io.htm (or... not. Just use 'w').
The silly_file variable is a powerhouse. It's not a string or a number variable. It's a whole bundle of features that lets us read from and write to the golf.txt file. We will use a feature called write, which will copy raw text or variable text into the text file we call "golf.txt". We will use the write function to copy a name and a score into the text file, as the exercise requires, when the time comes.
But wait! There's more! We need to get the names and the scores from the user, using keyboard input. This is not so bad, because we already learned how to retrieve keyboard input when we asked how many tournaments to record. Here's what to expect:
name = input("Enter name: ")
score = input("Enter score: ")
You can name the variables whatever you want, and write your own prompts, too! Since this exercise isn't requiring an exact match with an automatic tester, you can even make your prompts arcane and creepy, like "tell me your human name, o mortal golfer!"
To copy the name and the score into the text file, invoke the power of write:
silly_file.write(name + "\n")
silly_file.write(score + "\n")
The +"\n" puts a newline at the end of the name and the end of the score, the same as if the person pressed Enter. We do that so the text doesn't all appear on a single line.
Now we're close to being finished. We need to include code that makes Python repeat the process where we request name and score and write it to the text file, because Golf President may want five or ten weekends' worth of tournament winners recorded, we don't know yet. To cause a block of code to repeat you will need a for loop, which will also use a range function. You'll want to understand how for loops and range functions work in general. I recommend web searching for those tutorials, and I vouch for Tutorialspoint, python.org, w3schools and geek4geeks as solid sources. There are so many tutorials I won't recommend specific lessons. I will warn, however, lessons in Python for loops overlook that simply repeating a block of code is not necessarily the common use case for for-loops. For loops are more often used for iterating over items in a collection, but let's not go there right now. For now, I'll preempt your study by showing you an example for loop that repeat-executes a block of code with the help of a range function. Let this orient you if you during your study, because you will be distracted by tutorials that emphasize the for loop's other, more powerful utilities of iterating over items in collections, which you don't need.
my_number = 3
for i in range(my_number):
print("Hello")
# Hello
# Hello
# Hello
I think we have all the ingredients we need to make a happy golf president. I'll give you this pseudocode and wish you the greatest of luck.
YOUR CODE
-Say hello to the president, using the print function
-Request a number of tournaments to record, using the input function
-Convert the number of tournaments variable into a number variable, using the int function
-Use the -with- statement style to open a text file for writing
-Use the for-loop and range function to repeat a code block N times, where N is number of tournaments
-Use the input function to prompt/request a name, then request the score
-Use the file variable(silly_file?) write function to write the name you requested, then write the score you requested
You're done! OR ARE YOU?
I'd remark here that there is a possibility that your foolish user will not type in numbers when numbers are requested, and the program will crash. Some programming exercises will want you to detect when input is invalid, and re-prompt for input until the input is valid. That is a whole extra can of worms, and I think we opened too many cans of worms today. Since your exercise doesn't instruct you to check for valid input, I suggest that you don't, because it over-complicates the code.
I hope that helps!