11-15-2009, 07:36 PM
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.
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.
Finally, we add an "if" to assure the user that they input a number, and this is our final script
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
And now here is our "if" statement
and of course our "if not" statement
3. Write the user's input to a file.
This one is rather simple, first we prompt for input
then we create a file and specify that we will be writing into the file, the "w" means write
now you write the input to the file, and close the file, "x" is the input in the f.write(x)
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
now we open the file and declare that we are reading it, the "r" stands for read
Now we take the text in the file(line) and print it, then we close the file
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
Now we read what is already there and define a variable("z")
Now we combine what already there with the input
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.
Credits:
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')
Code:
def start():
x = raw_input('input a number')
if not x.isdigit
print 'This is not a number, input a number'
start() #Reprompt
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')
Code:
if x.isalpha():
print 'This is a letter'
raw_input()
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')
Code:
x = raw_input('input something')
f = open('file.txt', 'w')
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?')
Code:
x = raw_input('What is the name of your file?')
f = open(x , 'r')
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?')
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
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()
Credits:
Nevets04:
for thinking of the beginner projects
Fail Ninja:
for suggesting the idea to make a tut
Me:
for making the tut