Support Forums

Full Version: [TUT]Projects For Beginners 1-5[TUT]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Someone asked Nevets04 to make a tut for his beginner projects thread, but he has been busy with his beautiful company managment script so I though I would make the tut.

The projects post can be found here

This tutorial only covers 1-5 and I will post 6-10 later (well 6-9, 10 deserves its own thread)

1. Prompt the user to input a number, and if it is not a number re-prompt :

First we define a variable that prompts the user.
Code:
def start():
        x = raw_input('input a number')
Now x is equal to the users input, variables have the value "isdigit" which determines whether or not the input is a number. Now we add an "if not" to our script, and tell python what to do if x.isdigit is False.
Code:
def start():
        x = raw_input('input a number')                
        if not x.isdigit                                          
                print 'This is not a number, input a number'
                start() #Reprompt
Finally, we add an "if" to assure the user that they input a number, and this is our final script
Code:
def start():
        x = raw_input('input a number')
        if not x.isdigit():
                print 'This is not a number, input a number'
                start()
        if x.isdigit():
                print 'This is a number'
                raw_input()
start()


2. Prompt the user to enter a word, and if it is not a single word reprompt.

This is essentially the same as the above script, except instead of the "isdigit" value we will be using the "isalpha" value. Here is our input prompt
Code:
def start():
        x = raw_input('input a letter')
And now here is our "if" statement
Code:
if x.isalpha():
                print 'This is a letter'
                raw_input()
and of course our "if not" statement
Code:
def start():
        x = raw_input('input a letter')
        if x.isalpha():
                print 'This is a letter'
                raw_input()
        if not x.isalpha():
                print 'This is not a letter, input a letter'
                start()
start()


3. Write the user's input to a file.


This one is rather simple, first we prompt for input
Code:
x = raw_input('input something')
then we create a file and specify that we will be writing into the file, the "w" means write
Code:
x = raw_input('input something')
f = open('file.txt', 'w')
now you write the input to the file, and close the file, "x" is the input in the f.write(x)
Code:
x = raw_input('input something')
f = open('lol.txt', 'w')
f.write(x)
f.close()


4. Read the user's input from a file.


Now we will read a file and print it's contents. First we ask for the file name
Code:
x = raw_input('What is the name of your file?')
now we open the file and declare that we are reading it, the "r" stands for read
Code:
x = raw_input('What is the name of your file?')
f = open(x , 'r')
Now we take the text in the file(line) and print it, then we close the file
Code:
x = raw_input('What is the name of your file?')
f = open(x , 'r')
for line in f:
    print line
f.close()
raw_input()


5. Append the user's input to an existing file.


Now we add the users input to an already existing file with text in it. We ask for the file name, and what the user wants to add to the file
Code:
x = raw_input('what is the name of your file?')
y = raw_input('what do you want to add to your file?')
Now we read what is already there and define a variable("z")
Code:
x = raw_input('what is the name of your file?')
y = raw_input('what do you want to add to your file?')
f = open(x , 'r')
for line in f:
    z = line
Now we combine what already there with the input
Code:
x = raw_input('what is the name of your file?')
y = raw_input('what do you want to add to your file?')
f = open(x , 'r')
for line in f:
    z = line
f = open(x, 'w')
f.write(line + y)
f.close()
And that my tutorial for 1-5, I will do 6-9 as soon as possible and if you have any questions feel free to PM me. Blackhat Blackhat Blackhat
Credits:
Nevets04:
for thinking of the beginner projects

Fail Ninja:
for suggesting the idea to make a tut

Me:
for making the tut
Very nicely done.
Good work mate, This will get alot more people started on Python.