Projects for the Beginner

Reply   View First Unread View First Unread

Join Date: Apr 2007
Posts: 12
Reputation: StrikerX is an unknown quantity at this point 
Solved Threads: 0
StrikerX StrikerX is offline Offline
Newbie Poster

Re: Projects for the Beginner

 
0
  #121
May 19th, 2007
  1. #!bin/python
  2.  
  3. ###################################
  4. # Written by : StrikerX
  5. # Date : May 10 07
  6. # Purpose : simple Shell in Python
  7. ###################################
  8. from os import * # to use getcwd(), listdir(), chdir(), system() .
  9. from sys import * # to use exit().
  10.  
  11. print "Welcome to Python Shell ! "
  12.  
  13. command = ""
  14.  
  15. while (command != "exit"):
  16. command = raw_input(getcwd() + " %>#")
  17. command = command.strip()
  18. if command.strip()[:2] == 'cd':
  19. chdir(command.strip()[3:]) #getting the Path to be changed 2 !
  20. elif command == "ls":
  21. for x in listdir(getcwd()):
  22. print x
  23. else:
  24. system(command)
  25.  
  26. print "you are logging out of Python Shell ! "
  27. exit() #EXIT
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: ffao is an unknown quantity at this point 
Solved Threads: 4
ffao ffao is offline Offline
Newbie Poster

Re: Projects for the Beginner

 
0
  #122
May 21st, 2007
Originally Posted by sneekula View Post
Predict the outcome of this Python code:
  1. def sub_one(x):
  2. print "x before if -->", x
  3. if x > 1:
  4. x = x - 1
  5. sub_one(x)
  6. print "x after if -->", x
  7. return x
  8.  
  9. x = 5
  10. final = sub_one(x)
  11. print "final result -->", final
Can you explain the odd behaviour?
Easy. The call to sub_one(x) prints x, calls sub_one(x-1), and prints x-1!

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")
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,016
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 931
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Projects for the Beginner

 
0
  #123
May 26th, 2007
Crypting text is always fun, here is a simple rotation crypt (enough to make the NSA guys laugh) ...
  1. # simple rotation crypting of lowercase strings
  2.  
  3. import string
  4.  
  5. shift_right_by2 = string.maketrans(string.ascii_lowercase,
  6. string.ascii_lowercase[2:] + string.ascii_lowercase[:2])
  7.  
  8. # test --> cdefghijklmnopqrstuvwxyzab
  9. print string.ascii_lowercase[2:] + string.ascii_lowercase[:2]
  10.  
  11. shift_left_by2 = string.maketrans(string.ascii_lowercase,
  12. string.ascii_lowercase[-2:] + string.ascii_lowercase[:-2])
  13.  
  14. # test --> yzabcdefghijklmnopqrstuvwx
  15. print string.ascii_lowercase[-2:] + string.ascii_lowercase[:-2]
  16.  
  17. print "hello world".translate(shift_right_by2) # jgnnq yqtnf
  18. print "jgnnq yqtnf".translate(shift_left_by2) # hello world
  19.  
  20. # stay below 26 or you go full circle
  21. # --> abcdefghijklmnopqrstuvwxyz
  22. print string.ascii_lowercase[26:] + string.ascii_lowercase[:26]
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?
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: ffao is an unknown quantity at this point 
Solved Threads: 4
ffao ffao is offline Offline
Newbie Poster

Re: Projects for the Beginner

 
0
  #124
Jun 5th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Projects for the Beginner

 
0
  #125
Jun 16th, 2007
Here is code for simple word guessing game:
  1. # a very simple word guessing game
  2.  
  3. import random
  4.  
  5. def check(veil, guess, word):
  6. """
  7. check if the guess letter is in the word
  8. if found then update the veil
  9. """
  10. for ix, letter in enumerate(word):
  11. if letter == guess:
  12. veil[ix] = guess
  13. return veil
  14.  
  15. print "Guess a word (lower case letters only) ... "
  16.  
  17. words = ['art', 'bush', 'letter', 'banana', 'radio', 'hard',
  18. 'kiss', 'perl', 'target', 'fork', 'rural', 'zero', 'organ']
  19.  
  20. while True:
  21. # pick a word at random
  22. word = random.choice(words)
  23. tries = 0
  24. # create veil of dashes matching the word length
  25. veil = list('-' * len(word))
  26. while '-' in veil:
  27. print "".join(veil),
  28. guess = raw_input("Enter a letter: ").lower()
  29. veil = check(veil, guess, word)
  30. tries += 1
  31.  
  32. print "".join(veil)
  33. prompt = "You guessed correctly in %d tries, try again (y or n): " % tries
  34. tries = 0
  35. yn = raw_input(prompt).lower()
  36. if 'n' in yn:
  37. print "Thank you for playing!"
  38. break
Improve the game making sure each random word is picked only once. How would you add an hint to each of the words?
Last edited by bumsfeld; Jun 16th, 2007 at 9:25 pm. Reason: spell
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,016
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 931
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Projects for the Beginner

 
0
  #126
Jun 19th, 2007
This would be one way to create multiple stories from the same basic text ...
  1. # string templating is new in Python24
  2.  
  3. import string
  4.  
  5. def change_text(text, sub_dictionary):
  6. t = string.Template(text)
  7. return t.substitute(sub_dictionary)
  8.  
  9. text = 'The $animal says "$sound, my name is $name!"'
  10.  
  11. d_cow = dict(animal='cow', sound='mooh', name='Hilda')
  12. print change_text(text, d_cow)
  13.  
  14. d_dog = dict(animal='dog', sound='woof', name='Rover')
  15. print change_text(text, d_dog)
  16.  
  17. d_cat = dict(animal='cat', sound='meow', name='George')
  18. print change_text(text, d_cat)
  19.  
  20. print
  21.  
  22. # test the dictionary creation
  23. print d_cow # {'sound': 'mooh', 'name': 'Hilda', 'animal': 'cow'}
  24.  
  25. """
  26. result -->
  27. The cow says "mooh, my name is Hilda!"
  28. The dog says "woof, my name is Rover!"
  29. The cat says "meow, my name is George!"
  30. """
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,016
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 931
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Projects for the Beginner

 
0
  #127
Jun 23rd, 2007
If you read the news from Europe, you will find that temperatures are given in degrees Celcius and rain fall is measured in liters per squaremeter. Write a program the converts between Celcius and Fahrenheit, also between liters per squaremeter and inches of rain.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Address book

 
0
  #128
Jul 5th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,016
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 931
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Projects for the Beginner

 
0
  #129
Jul 20th, 2007
  1. # the numbers in this list have the same sum and product
  2. lst = [0.5, 1.2, 3.75, 4.36]
  3.  
  4. # test the assertion ...
  5. print 0.5 * 1.2 * 3.75 * 4.36 # 9.81
  6. print sum(lst) # 9.81
1) Test this assertion using a for loop.
2) Can you come up with other such lists, using Python to help you?
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Projects for the Beginner

 
0
  #130
Jul 23rd, 2007
This little code could be beginning of a scrambled sentence game:
  1. # simple sentence scrambler
  2.  
  3. import random
  4.  
  5. def scatter_word(a_word):
  6. """
  7. scramble a word, and leave words less than 4 characters long,
  8. and the beginning and ending characters of longer words alone
  9. """
  10. if len(a_word) < 4:
  11. return a_word
  12. else:
  13. # create a list of interior characters
  14. t = list(a_word[1:-1])
  15. # shuffle the characters
  16. random.shuffle(t)
  17. # join back to form a word
  18. return a_word[0] + "".join(t) + a_word[-1]
  19.  
  20. def scatter_text(text):
  21. return " ".join(scatter_word(x) for x in text.split(" "))
  22.  
  23.  
  24. text = "A goldfish has a memory span of three seconds"
  25. print scatter_text(text)
  26.  
  27. """
  28. possible result -->
  29. A gfiosldh has a momery sapn of trehe sondecs
  30. """
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Reply

Tags
beginner, projects, python

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC