| | |
Projects for the Beginner
![]() | View First Unread |
•
•
Join Date: Apr 2007
Posts: 12
Reputation:
Solved Threads: 0
Python Syntax (Toggle Plain Text)
#!bin/python ################################### # Written by : StrikerX # Date : May 10 07 # Purpose : simple Shell in Python ################################### from os import * # to use getcwd(), listdir(), chdir(), system() . from sys import * # to use exit(). print "Welcome to Python Shell ! " command = "" while (command != "exit"): command = raw_input(getcwd() + " %>#") command = command.strip() if command.strip()[:2] == 'cd': chdir(command.strip()[3:]) #getting the Path to be changed 2 ! elif command == "ls": for x in listdir(getcwd()): print x else: system(command) print "you are logging out of Python Shell ! " exit() #EXIT
•
•
Join Date: May 2007
Posts: 15
Reputation:
Solved Threads: 4
•
•
•
•
Predict the outcome of this Python code:
Can you explain the odd behaviour?python Syntax (Toggle Plain Text)
def sub_one(x): print "x before if -->", x if x > 1: x = x - 1 sub_one(x) print "x after if -->", x return x x = 5 final = sub_one(x) print "final result -->", final
x before if --> 5
x before if --> 4
x before if --> 3
x before if --> 2
x before if --> 1 (the if is not executed here)
x after if --> 1
x after if --> 1
x after if --> 2
x after if --> 3
x after if --> 4
final result --> 4 (the only modification made to x is "x = x - 1")
Crypting text is always fun, here is a simple rotation crypt (enough to make the NSA guys laugh) ...
You can try other shift values from 1 to 25. If you use 26, you go once around the circle of course. How would you include capital letters?
python Syntax (Toggle Plain Text)
# simple rotation crypting of lowercase strings import string shift_right_by2 = string.maketrans(string.ascii_lowercase, string.ascii_lowercase[2:] + string.ascii_lowercase[:2]) # test --> cdefghijklmnopqrstuvwxyzab print string.ascii_lowercase[2:] + string.ascii_lowercase[:2] shift_left_by2 = string.maketrans(string.ascii_lowercase, string.ascii_lowercase[-2:] + string.ascii_lowercase[:-2]) # test --> yzabcdefghijklmnopqrstuvwx print string.ascii_lowercase[-2:] + string.ascii_lowercase[:-2] print "hello world".translate(shift_right_by2) # jgnnq yqtnf print "jgnnq yqtnf".translate(shift_left_by2) # hello world # stay below 26 or you go full circle # --> abcdefghijklmnopqrstuvwxyz print string.ascii_lowercase[26:] + string.ascii_lowercase[:26]
May 'the Google' be with you!
•
•
Join Date: May 2007
Posts: 15
Reputation:
Solved Threads: 4
Luckily, I have kept most of the useless stuff I wrote when I was a beginner. I knew they helped a lot, even though they were quite trivial, and note that I'm listing everything here (even the silliest!)
1) Create a proggie called "even" to print, for a range of numbers, whether they're even or odd. Make the range user-specified. Generalise so the user can input any divisor they like to check for (not only 2). Generalise so the user can not only input a range of numbers, but an arithmetic progression.
2) Create a program called "exp". It should ask for two numbers and then do an exponentiation, without using the exponentiation operator
(flow control).
3) Generate a random string. Make the length user-specified. Make the set of characters user-changeable.
4) Create a program to print the UTC time. Change it to your local time.
5) Create a base-converter.
1) Create a proggie called "even" to print, for a range of numbers, whether they're even or odd. Make the range user-specified. Generalise so the user can input any divisor they like to check for (not only 2). Generalise so the user can not only input a range of numbers, but an arithmetic progression.
2) Create a program called "exp". It should ask for two numbers and then do an exponentiation, without using the exponentiation operator
(flow control).
3) Generate a random string. Make the length user-specified. Make the set of characters user-changeable.
4) Create a program to print the UTC time. Change it to your local time.
5) Create a base-converter.
Here is code for simple word guessing game:
Improve the game making sure each random word is picked only once. How would you add an hint to each of the words?
python Syntax (Toggle Plain Text)
# a very simple word guessing game import random def check(veil, guess, word): """ check if the guess letter is in the word if found then update the veil """ for ix, letter in enumerate(word): if letter == guess: veil[ix] = guess return veil print "Guess a word (lower case letters only) ... " words = ['art', 'bush', 'letter', 'banana', 'radio', 'hard', 'kiss', 'perl', 'target', 'fork', 'rural', 'zero', 'organ'] while True: # pick a word at random word = random.choice(words) tries = 0 # create veil of dashes matching the word length veil = list('-' * len(word)) while '-' in veil: print "".join(veil), guess = raw_input("Enter a letter: ").lower() veil = check(veil, guess, word) tries += 1 print "".join(veil) prompt = "You guessed correctly in %d tries, try again (y or n): " % tries tries = 0 yn = raw_input(prompt).lower() if 'n' in yn: print "Thank you for playing!" break
Last edited by bumsfeld; Jun 16th, 2007 at 9:25 pm. Reason: spell
Should you find Irony, you can keep her!
This would be one way to create multiple stories from the same basic text ...
python Syntax (Toggle Plain Text)
# string templating is new in Python24 import string def change_text(text, sub_dictionary): t = string.Template(text) return t.substitute(sub_dictionary) text = 'The $animal says "$sound, my name is $name!"' d_cow = dict(animal='cow', sound='mooh', name='Hilda') print change_text(text, d_cow) d_dog = dict(animal='dog', sound='woof', name='Rover') print change_text(text, d_dog) d_cat = dict(animal='cat', sound='meow', name='George') print change_text(text, d_cat) # test the dictionary creation print d_cow # {'sound': 'mooh', 'name': 'Hilda', 'animal': 'cow'} """ result --> The cow says "mooh, my name is Hilda!" The dog says "woof, my name is Rover!" The cat says "meow, my name is George!" """
May 'the Google' be with you!
I don't know if this idea has already been added, but beginners can create an address book. That was the first real project I undertook. Ask the user for: name, address, phone, cell, fax, etc. Then store the data in a .txt file. Let the user search people in the list, create new listings, delete listings, and edit existing ones.
Last edited by shadwickman; Jul 5th, 2007 at 11:04 pm.
python Syntax (Toggle Plain Text)
# the numbers in this list have the same sum and product lst = [0.5, 1.2, 3.75, 4.36] # test the assertion ... print 0.5 * 1.2 * 3.75 * 4.36 # 9.81 print sum(lst) # 9.81
2) Can you come up with other such lists, using Python to help you?
May 'the Google' be with you!
This little code could be beginning of a scrambled sentence game:
python Syntax (Toggle Plain Text)
# simple sentence scrambler import random def scatter_word(a_word): """ scramble a word, and leave words less than 4 characters long, and the beginning and ending characters of longer words alone """ if len(a_word) < 4: return a_word else: # create a list of interior characters t = list(a_word[1:-1]) # shuffle the characters random.shuffle(t) # join back to form a word return a_word[0] + "".join(t) + a_word[-1] def scatter_text(text): return " ".join(scatter_word(x) for x in text.split(" ")) text = "A goldfish has a memory span of three seconds" print scatter_text(text) """ possible result --> A gfiosldh has a momery sapn of trehe sondecs """
Should you find Irony, you can keep her!
![]() |
Similar Threads
- Ideal project for a beginner? (Python)
- Hep finding C++ Projects (Python)
- Help finding C++ Projects (C++)
- New task from Projects for the beginner: (Python)
Other Threads in the Python Forum
- Previous Thread: Calling Function?
- Next Thread: How to avoid python shebang line on linux.
| Thread Tools | Search this Thread |
.so abrupt address advice anti apax avogadro beginner c++ calculator code college coordinates cturtle curved def development edit embed enter erp event examples excel file forms function google gui hints http ideas iframe input introduction itconsulting itunes j2seprojects jaunty java javaprojects library linux list lists loan maze microsoft mouse movingimageswithpygame mysql mysqldb newbie number opensource parameters path php prime programming project projects push py2exe pygame pygtk python random read remote rubyconf silverlight simple smtp softwaredevelopment string sudokusolver sum suthar syntax systemintegration table tennis terminal threading tkinter tlapse tooltip tricks tutorial ubuntu urllib urllib2 variable ventrilo web-scrape wikipedia wordgame wxpython xlwt








