Click to See Complete Forum and Search --> : this is a weird request, but i'm weird too so it's alright...
inkedmn
12-03-2001, 05:52 PM
i need ideas for things i can code to practice python. i'm as NEWBIE as the day is long, so try to keep them basic (even TOO basic would be fine, just so i can feel like i'm doing things)...
TLD - this is just for the times when i'm stuck and you're actually doing what you get paid to do instead of helping my sorry butt :).
thanks all
and TLD is rad for all the help he's giving me...
[ 04 December 2001: Message edited by: inkedmn ]
Gnu/Vince
12-03-2001, 10:16 PM
Go to http://darkhost.mine.nu:81 and grab my Ruby example. They are commented. Try to translate them to Python. Most of them are very simple.
George Kilroy
12-04-2001, 09:00 PM
Hah, I just did a rock/paper/scissors game in python yesterday. The computer always wins. :D
inkedmn
12-04-2001, 09:45 PM
Originally posted by George Kilroy:
<STRONG>Hah, I just did a rock/paper/scissors game in python yesterday. The computer always wins. :D</STRONG>
post it!
George Kilroy
12-04-2001, 10:03 PM
a = 0
b = 0
def lose():
print "Sorry, but you lose."
while a == 0:
c = raw_input("Do you want (r)ock, (p)aper, or (s)cissors? ")
if c == "r" or c == "R" or c == "p" or c == "P" or c == "s" or c == "S":
if c == "r" or c == "R":
print "Paper covers rock."
lose()
a = 1
elif c == "p" or c == "P":
print "Scissors cuts paper."
lose()
a = 1
else:
print "Rock crushes scissors."
lose()
a = 1
else:
print "What?"
There is probably a way to make the script shorter, but I couldn't figure anything out.
George Kilroy
12-04-2001, 10:05 PM
Why doesn't....
if c == "r" or "R" or "p" or "P" "s" or "S":
.....work for line 7?
[ 04 December 2001: Message edited by: George Kilroy ]
George Kilroy
12-04-2001, 10:21 PM
I was thinking of using my R/P/S game and try to make the computer actually have to "think" about which move is best, rather than me telling it which is. I don't know how to go about doing it, but I figured this might be the format:
# 1st: The player types in his message.
Input text....
Player said: rock
# 2nd Computer will be given the rules of the game:
ROCK
can be COVERed
can CRUSH
PAPER
can be CUT
can COVER
SCISSOR
can be CRUSHED
can CUT
BEING CRUSHED, CUT, or COVERED is bad!
# 3rd computer makes a random guess,
# then it sees if the guess will make it win.
Computer guesses: SCISSORS.
computer thinks:
SCISSORS can be CRUSHED by the user's ROCKS
== bad!
# 4th if choice was bad! then the computer
# trys another random guess (minus the guess
# it just made) until it makes a good option.
Is this weird enough for you?
[ 04 December 2001: Message edited by: George Kilroy ]
Gnu/Vince
12-05-2001, 12:26 AM
Here's my rock paper cisor. This is random, not with a cheater computer:
#!/usr/bin/env ruby
=begin
rpc.rb - copyright (c) 2001, Vincent Foley-Bourgon <gnuvince@yahoo.ca>
This program is lisenced under the GNU General Public License (GPL)
<http://www.gnu.org/copyleft/gpl.html>
=end
# Les armes
Weapons = ["Roche", "Papier", "Ciseau"]
# Qui gagne sur qui?
Winners = Hash["Roche" => "Ciseau" , "Papier" => "Roche" , "Ciseau" => "Papier"] # Initialisation des scores finaux
$score_joueur = 0
$score_ordi = 0 # Fonction qui affiche le message d'erreur
def error_msg
puts "\nOption Invalide"
puts "Press Enter to continue..."
gets
end # Fonction pour déterminer le gagnant
def determiner_gagnant(joueur, ordi) # Partie nulle si le joueur et l'ordinateur ont la même arme
if joueur == ordi
return "Partie Nulle" # Si le joueur a l'arme qu'il faut pour battre l'ordinateur, il gagne
elsif Winners[joueur] == ordi
$score_joueur += 1
return "Tu gagnes!!!!" # Si l'ordinateur a l'arme qu'il faut pour battre le joueur, il gagne
elsif Winners[ordi] == joueur
$score_ordi += 1
return "Ordinateur gagne :-(" # Exceptions. Ça arrivera jamais
else
ArgumentError "Hrmmm... kekchose de BEN weird est arrivé: #{joueur}, #{ordi}"
end
end # On arrête pas de jouer tant que l'utilisateur choisit pas l'option 0
option = nil
while option != 0 do # Clear screen and put a nice and dumb menu
system("clear")
print "\t\t\t Roche-Papier-Ciseau en Ruby\n\n"
puts "1. Roche"
puts "2. Papier"
puts "3. Ciseau"
puts "\n0. Quitter"
print "\nOption: " # On s'assure que l'usager n'entre qu'un seul chiffre (pas de lettre)
if not option = /^\s*(\d)\s*$/.match (gets) then
error_msg
next
end
# On assigne au joueur son arme
choix_joueur =
case option[1].to_i
when 1 then Weapons[0]
when 2 then Weapons[1]
when 3 then Weapons[2]
when 0 then break
else
error_msg
next
end # Choix de l'arme pour l'ordinateur
choix_ordi = Weapons[rand(3)]
# On appelle la fonction determiner_gagnant pour trouver qui est le
# gagnant et on met le résultat dans 'resultat'
resultat = determiner_gagnant(choix_joueur, choix_ordi) # Affichage de l'arme des 2 joueurs ainsi que de qui gagne
puts "\nJoueur a choisit: #{choix_joueur}"
puts "Ordinateur a choisit: #{choix_ordi}"
puts resultat; gets end # Affichage du score final
puts
puts "Score final"
puts "Ordinateur : #{$score_ordi}"
puts "Joueur : #{$score_joueur}"
Aragorn
12-05-2001, 02:25 AM
Originally posted by George Kilroy:
<STRONG>Why doesn't....
if c == "r" or "R" or "p" or "P" "s" or "S":
.....work for line 7?
[ 04 December 2001: Message edited by: George Kilroy ]</STRONG>
because between your "P" and "s" there is no or... ;)
Aragorn
Aragorn
12-05-2001, 02:33 AM
Originally posted by inkedmn:
<STRONG>i need ideas for things i can code to practice python. i'm as NEWBIE as the day is long, so try to keep them basic (even TOO basic would be fine, just so i can feel like i'm doing things)...
TLD - this is just for the times when i'm stuck and you're actually doing what you get paid to do instead of helping my sorry butt :).
thanks all
and TLD is rad for all the help he's giving me...
[ 04 December 2001: Message edited by: inkedmn ]</STRONG>
Do a calculator that has more than just your regular simple functions.
You could build a small database using shelve that stores your cd/mp3/dvd or whatever collection then writes it to a file in a formatted way.
You could write a small email client for yourself that checks your pop3 accounts..(this is MUCH easier than it sounds), you could even learn threads this way by allowing you to write an email while it still checks it at a fixed time like every 5 minutes or so. This would also be a good way to teach you the time library, it kinda threw me for a loop when I first used it.
If I was going to recommend any of them to teach you some GREAT fundamentals of Python I would recommend the the email program. It can allow for learning file access, database access(if you so choose), time library, regular expressions as you have to parse the header of each email, threads(if you so choose). It can be a fun program to write, I should know that is what I did for my second program! ;)
Aragorn