Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 31

Thread: Tell me...

  1. #21

    Re: Tell me...

    Nobody's forcing you to use Python. I rather like it. I may try ruby someday if I get time, but it's not a very high priority, especially with the other things I have going on (setting up home network, converting mp3s to oggs, learning perl, etc).

  2. #22
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Re: Tell me...

    Nah, better yet - send a 12v zap each time the moron compiles without any commets (and a patch at the top doesnt count in my book)

    After a while (unless you happen to be Homer SImpson) ya get the idea that either A) Stop programming because it hurts or B) the computer is pissed -- make it happy

    Just my evil and sadistic solution to the race of Stupid people.



    How about general flame to anyone who doesn't comment?

  3. #23
    Mentor coltrane's Avatar
    Join Date
    May 2001
    Location
    North Carolina
    Posts
    1,390

    Re: Tell me...

    GnuVince said:
    How about just one?
    Feztaa said :
    Because then the language would be really boring and restrictive. Different methods of doing things can provide different functionality.
    While true Feztaa, this also means that it creates hell for the programmer that comes behind you and has to debug your work. Why waste 10 lines of code with something that you can accomplish with just 1?

  4. #24

    Re: Tell me...

    While true Feztaa, this also means that it creates hell for the programmer that comes behind you and has to debug your work. Why waste 10 lines of code with something that you can accomplish with just 1?
    Possibly because the 10 line method provides additional functionality that you wouldn't otherwise have in the 1 line method.

    Or, possibly because the 10 line method was written by a bad programmer

  5. #25
    Guest

    Re: Tell me...

    I'm continuing to experiment Python. *I've gotten to the point that the syntax almost doesn't bother me anymore. *But, I must say that I would like the python indent file of Vim to be a little bit more featureful.

    I submitted a Rock-Paper-Cisor example to codeexamples.org, here it is, tell me what you think.

    Code:
    #!/usr/bin/env python
    
    # rpc.py - copyright (c) 2001, Vincent Foley-Bourgon <gnuvince@yahoo.ca>
    # 
    # This program is lisenced under the terms of the GNU General Public Lisence
    # <http://www.gnu.org/copyleft/gpl.html>
    
    # We will be needing random function
    import random
    
    # We first create an array containing the possible weapons (these are
    # indexed from 0 to 2.
    Weapons = ["Rock", "Paper", "Cisor"]
    
    # Next we create a dictionnary in which we put the winning relations.
    Winners = {"Rock" : "Cisor" , "Paper" : "Rock" , "Cisor" : "Paper"}
    
    # I use global variables for the scores. *Some may say it's bad programming
    # practice, but it's still useful to know how to do them.
    score_player = 0
    score_cpu = 0
    
    # This function will display an evil error message if the user did some
    # vile thing and will then wait for the user to press Enter.
    def error_msg():
     *print "\nInvalid option"
     *print "Please press enter to continue"
     *raw_input()
    
    # This function determines who wins the current round. *It recieves two
    # parameters: the weapon of player and the weapon of the computer
    def determine_winner(player, cpu):
    
     *# Define that these 2 variables are global
     *global score_cpu
     *global score_player
     *
     *# If both weapons are the same, return "Tie!"
     *if player == cpu:
     * *return "Tie!"
    
     *# If the weapon that player's weapon beat is the same than the computer's
     *# weapon, we add one to the score of player and we return a message saying
     *# that player is the winner
     *elif Winners[player] == cpu:
     * *score_player += 1
     * *return "You win!!!"
    
     *# Same as above, except that now we check if the computer beats the player.
     *elif Winners[cpu] == player:
     * *score_cpu += 1
     * *return "Computer wins... :("
    
    
    # Main function
    def main():
    
     *# We must initialize option, else the program will crash on the next line
     *option = ""
    
     *# We play until the user inputs option 0
     *while option != 0:
    
     * *# Print and nice and dumb menu and ask for input
     * *print "\n\t\t\t Rock-Paper-Cisor in Python \n"
     * *print "1. Rock"
     * *print "2. Paper"
     * *print "3. Cisor"
     * *print "\n0. Quit"
    
     * *# We ask for input here. *If the user does something bad like Ctrl-C
     * *# or Ctrl-D, we display an error message.
     * *try:
     * * *option = raw_input('Option: ')
     * *except:
     * * *error_msg()
     * * *continue
    
     * *# If we can't convert the user's input to an integer, it's probably because
     * *# He input something else than a number. *In that case, we display
     * *# our evil error message and we then redisplay the menu
     * *try:
     * * *option = int(option)
     * *except:
     * * *error_msg()
     * * *continue
     * * *
    
     * *# We give a weapon to the player accordingly to the option he chose.
     * *# if he chose an invalid option, we display and evil error message.
     * *# and we redisplay the menu
     * *if option == 1:
     * * *weapon_player = Weapons[0]
     * *elif option == 2:
     * * *weapon_player = Weapons[1]
     * *elif option == 3:
     * * *weapon_player = Weapons[2]
     * *elif option == 0:
     * * *break
     * *else:
     * * *error_msg()
     * * *continue
    
     * *# Give the computer a random weapon
     * *weapon_cpu = Weapons[random.randrange(3)]
    
     * *# Let's see who wins!
     * *result = determine_winner(weapon_player, weapon_cpu)
    
     * *# Print what each player took and display the result of the round
     * *print "\nPlayer chose *:", weapon_player
     * *print "Computer chose:",weapon_cpu
     * *print result
    
     *# When the user quits, display the final score
     *print
     *print "Final Score"
     *print "Player * * :",score_player
     *print "Computer * :",score_cpu
     * * *
    
    if __name__ == '__main__':
     *main()

  6. #26
    Guest

    Re: Tell me...

    For people who are interested, here's the same thing in Ruby (it's been sumbitted too):
    Code:
    #!/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
    
    # I use global variables for the scores. *Some may say it's bad programming
    # practice, but it's still useful to know how to do them. *In Ruby, just put
    # a '$' sign before the name
    $score_player = 0
    $score_cpu = 0
    
    
    
    # This function will display an error message and will then wait for the user
    # to press Enter
    def error_msg
     *puts "\nInvalid option" 
     *puts "Press Enter to continue..."
     *gets
    end
    
    
    # This function will determine who is the winner of the current round
    # We recieved the weapon of player and of the computer
    def determine_winner(player, cpu)
    
     *# If both players have the same weapon, we return "Tie!"
     *if player == cpu
     * *"Tie!"
    
     *# OK, this is a tricky part. *We created a hash that tells what wins over
     *# what. *'put Winners["Rock"]' will print "Cisor". *So we check if the 
     *# weapon of the computer is the same as what the Weapon of the player beats
     *# If it beats it, we give one point to player and we return "Player Wins!!"
     *elsif Winners[player] == cpu
     * *$score_player += 1
     * *"Player Wins!!!"
    
     *# Same thing than above except that now we check if the player lost.
     *elsif Winners[cpu] == player
     * *$score_cpu += 1
     * *"Computer wins... :("
     *end
    end
    
    
    # MAIN FUNCTION
    
    # The weapons that we can use
    Weapons = ["Rock", "Paper", "Cisor"]
    
    # What weapon beats whatr other weapon
    Winners = Hash["Rock" => "Cisor" , "Paper" => "Rock" , "Cisor" => "Paper"]
    
    # We initialize option, else we will get a bug on the next line
    option = nil
    
    # While the user doesn't input 0, we keep looping (playing)
    while option != 0 do
    
     *# Clear screen and put a nice and dumb menu
     *system("clear")
     *print "\t\t\t Rock-Paper-Cisor Ruby\n\n"
     *puts "1. Rock"
     *puts "2. Paper"
     *puts "3. Cisor"
     *puts "\n0. Quit"
     *print "\nOption: "
    
     *# We make sure that the user only inputs digits. *There are two ways to do
     *# this. *I left the first one in comment
     *# *First way: input filter with a regular expression: we check if the user
     *# *only input one integer (there may be spaces around). If not we display
     *# *an error message
     *#
     *# *if not option = /^\s*(\d)\s*$/.match (gets) then
     *# * *error_msg
     *# * *next
     *# *end
    
     *# Here I used an exception. *Right now, I am forced to put gets.chomp,
     *# because if the user inputs '0', Kernel#Integer will think that it's the
     *# beginning of an octal number. This has been fixed in a very recent version
     *# of Ruby
     *#
     *# So we get our option and try to transform it into an integer. *If this
     *# fails, we display an error message and we go to the 'end do' which will
     *# bring us our menu back.
     *begin
     * *option = Integer(gets.chomp)
     *rescue
     * *error_msg
     * *next
     *end
     *
     *# We give the user the weapon of his choice
     *weapon_player = case option
     * *when 1 then Weapons[0]
     * *when 2 then Weapons[1]
     * *when 3 then Weapons[2]
     * *when 0 then break
     * *else 
     * * *error_msg
     * * *next
     *end
    
     *# We get the weapon for the computer
     *weapon_cpu = Weapons[rand(3)]
     *
     *# We call the function determine_winner to know who wins. *We send that
     *# function 2 parameters: the weapon of the player and of the computer
     *result = determine_winner(weapon_player, weapon_cpu)
    
     *# We display who won
     *puts "\nPlayer chose: #{weapon_player}"
     *puts "Computer chose: #{weapon_cpu}"
     *puts result; gets
    
    end
    
    # Print final score
    puts
    puts "Final Score"
    puts "Computer : #{$score_cpu}"
    puts "Player : #{$score_player}"

  7. #27

    Re: Tell me...

    I'd like to set the record straight and say python sucks. ;D

  8. #28
    Guest

    Re: Tell me...


    I'd like to set the record straight and say python sucks. *;D
    Thank you Perl troll...

    :eww: Perl :eww:

  9. #29
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Re: Tell me...

    Dont hate perl, hate the dickhead programmer. I love to use perl, jsut hate reading the work of idiots (which the world is full of. Proof ?? -- WindowsXP)

  10. #30
    Mentor coltrane's Avatar
    Join Date
    May 2001
    Location
    North Carolina
    Posts
    1,390

    Hmmmm

    It doesnt look that bad, just by looking at it though.

    Not on my list of things to learn though.

    Think Im going to get my HP Cert first, then maybe Cisco or something. Remaster C++

    then play with Python

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •