Week #4 Challenge: Python

  • (8 Pages)
  • +
  • « First
  • 3
  • 4
  • 5
  • 6
  • 7
  • Last »

108 Replies - 15910 Views - Last Post: 06 September 2011 - 12:05 PM

#61 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Week #4 Challenge: Python

Posted 26 January 2010 - 04:03 PM

I didn't knew you could use +=, but yea the returning way its much better I'm just learning and thanks for the overlook :P
Was This Post Helpful? 0
  • +
  • -

#62 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 929
  • View blog
  • Posts: 6,316
  • Joined: 18-October 08

Re: Week #4 Challenge: Python

Posted 26 January 2010 - 07:25 PM

You are all doing some great stuff here. Look forward to seeing some more.
Was This Post Helpful? 0
  • +
  • -

#63 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Week #4 Challenge: Python

Posted 26 January 2010 - 08:15 PM

View Postprogramble, on 26 Jan, 2010 - 03:00 PM, said:

def password(length):
    passw = ""
    for i in range(length): # Count to length
        passw += random.choice('0123456789abcdefghijklmnopqrstuvwxyz')
    return passw # return instead of print, so we can call it from another function and store the password for example



import string
def password(length):
    return "".join([random.choice(string.printable[:62]) for x in range(length)])




Edited: all good now.

This post has been edited by SpeedisaVirus: 26 January 2010 - 08:38 PM

Was This Post Helpful? 0
  • +
  • -

#64 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 19
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Re: Week #4 Challenge: Python

Posted 27 January 2010 - 06:41 AM

Just put together a quick command line game: The Game of Nim.

Quote

The game of Nim is played with 3 piles of stones. There are three stones in the first pile, five stones in the second, and eight stones in the third. Two players alternate taking as many stones as they like from anyone pile. Play continues until someone is forced to take the last stone. The person taking the last stone loses.


My code:

ONE = 1 # player one
TWO = 2 # player two

piles = [3,5,8] # number of stones in each pile respectively.
turn = ONE

# shows currently how many stones are in each pile
def showPiles():
	print "Pile1: ", piles[0], " Pile2: ", piles[1], " Pile3: ", piles[2]

# checks if the game is over.
# the game is over if all 3 piles are empty
# this funciton returns 1 if the game is over and 2 otherwise
def isGameOver():
	if piles[0] == 0 and piles[1] == 0 and piles[2] == 0:
		return 1
	return 2

showPiles()

while isGameOver() == 2:

	#keep check until the user chooses a valid pile that is not empty
	while 1 == 1:
		pile = int(raw_input("Player " + str(turn) + " Choose a pile: "))

		if(pile >= 1 and pile <= 3):
			if piles[pile-1] > 0:
				break #you have chosen a valid pile
			else:
				print "You have chosen an empty pile."
		else:
			print "Invalid pile! Please choose a pile between 1 and 3"

	

	# keep going until the user chooses a valid number of stones
	# i.e. the user cannot take more stones than our in the pile
	# and the user cannot take a negative number of stones
	while 1==1:
		stones = int(raw_input("How many stones would you like to take?"))
		if(piles[pile-1] >= stones):
			piles[pile-1] -= stones
			break
		else:
			print "You entered an invalid amount of stones."

	showPiles()

	if piles[0] == 0 and piles[1] == 0 and piles[2] == 0:
		# game over
		if turn == ONE:
			# player one took the last stone, player 2 wins
			print "Player two wins!"
		else:
			# player two took the last stone, player 1 wins
			print "Player one wins"
	else:
		# change the player
		if turn == ONE:
			turn = TWO
		else:
			turn = ONE

	print



Not bad, for my first program with Python. :)
Was This Post Helpful? 0
  • +
  • -

#65 Scorpiion  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 117
  • Joined: 23-April 09

Re: Week #4 Challenge: Python

Posted 27 January 2010 - 07:36 AM

I'm thinking of a simple game as I said before, I'm looking at some others games right now for insperation but now I have a question. Is there any place where you can download "GPL/BSD -like" sound effects? Or can I take some from some open source game? (I will do my game open source of course.. but if there is like a site for free sound effects that would be nice) :)
Was This Post Helpful? 0
  • +
  • -

#66 marmoor1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-August 09

Re: Week #4 Challenge: Python

Posted 27 January 2010 - 09:28 AM

Hello, this is my first python program. A number guessing game.

import random
number = random.randint(1,100)
print("Try to guess the number. It's between 1 and 100. You have only 10 attempts!")
counter = 0

while counter < 10:
	counter = counter + 1
	answer = int(input())
	
	if( answer < number ):
		print("Too Low!")
		attemptsLeft = 10 - counter
		print ("You have ", attemptsLeft ," attempts left")
		
	elif( answer > number ):
		print("Too High")
		print ("You have ", attemptsLeft ," attempts left")
		
	else:
		print("Correct!")
		break
if counter >= 10:
	print("You lose!")

Was This Post Helpful? 0
  • +
  • -

#67 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 19
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Re: Week #4 Challenge: Python

Posted 27 January 2010 - 12:31 PM

Here is an idea to add lists to the above program: don't allow the user to use a number that they already asked.
Was This Post Helpful? 1
  • +
  • -

#68 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 19
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Re: Week #4 Challenge: Python

Posted 27 January 2010 - 12:54 PM

Also, what is the big difference between input and raw_input?
Was This Post Helpful? 0
  • +
  • -

#69 marmoor1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-August 09

Re: Week #4 Challenge: Python

Posted 27 January 2010 - 01:54 PM

View Postchili5, on 27 Jan, 2010 - 11:31 AM, said:

Here is an idea to add lists to the above program: don't allow the user to use a number that they already asked.


Good idea! Thank you man :) I did what you suggested. Here is what I came up with:
import random
number = random.randint(1,100)
print("Try to guess the number. It's between 1 and 100. You have only 10 attempts!")
counter = 0
numbersInserted = []
x = 1

while counter < 10:
	counter = counter + 1
	answer = int(input())
	x = 1

	for i in range(len(numbersInserted)):
		if  numbersInserted[i] == answer :
			print("You've already inserted this number!")
			x = 0
			break
		
	if( x == 1 ):		
		numbersInserted.append(answer)
		
		if( answer < number ):
			print("Too Low!")
			attemptsLeft = 10 - counter
			print ("You have ", attemptsLeft ," attempts left")
			
		elif( answer > number ):
			print("Too High")
			print ("You have ", attemptsLeft ," attempts left")
			
		else:
			print("Correct!")
			break
if counter >= 10:
	print("You lose!")


Was This Post Helpful? 0
  • +
  • -

#70 marmoor1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-August 09

Re: Week #4 Challenge: Python

Posted 27 January 2010 - 02:09 PM

View Postchili5, on 27 Jan, 2010 - 11:54 AM, said:

Also, what is the big difference between input and raw_input?


I'm using Python 3. "raw_input" is removed from Python 3.
Was This Post Helpful? 0
  • +
  • -

#71 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 19
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Re: Week #4 Challenge: Python

Posted 27 January 2010 - 02:27 PM

Nice!

	for i in range(len(numbersInserted)):
		if  numbersInserted[i] == answer :
			print("You've already inserted this number!")
			x = 0
			break



I think there is a better way to do that though.

Try something like this:

if nGuess in guesses:
		print "You already guessed ", nGuess



That works for me in Python 2.6. Try it in 3?
Was This Post Helpful? 1
  • +
  • -

#72 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Week #4 Challenge: Python

Posted 27 January 2010 - 02:42 PM

In python 2, raw_input is used for input, Python 3 uses input().
chili5, I edited your code a bit, added some checks to make sure the user enters a number, and used True and False in some places instead of 1 or 2. Hope this helps.


ONE = 1 # player one
TWO = 2 # player two

piles = [3,5,8] # number of stones in each pile respectively.
turn = ONE

# shows currently how many stones are in each pile
def showPiles():
    print "Pile1: ", piles[0], " Pile2: ", piles[1], " Pile3: ", piles[2]

# checks if the game is over.
# the game is over if all 3 piles are empty
# this funciton returns True if the game is over and False otherwise
def isGameOver():
    if piles[0] == 0 and piles[1] == 0 and piles[2] == 0:
        return True
    return False

showPiles()

while isGameOver() == False:

    #keep check until the user chooses a valid pile that is not empty
    while True:
        # What if the user does not enter a number?
        try:
            pile = int(raw_input("Player " + str(turn) + " Choose a pile: "))
        except ValueError:
            print "Please enter a number"
            # Start over
            continue
        
        if(pile >= 1 and pile <= 3):
            if piles[pile-1] > 0:
                break #you have chosen a valid pile
            else:
                print "You have chosen an empty pile."
        else:
            print "Invalid pile! Please choose a pile between 1 and 3"

   

    # keep going until the user chooses a valid number of stones
    # i.e. the user cannot take more stones than our in the pile
    # and the user cannot take a negative number of stones
    while True:
        # What if the user does not enter a number?
        try:
            stones = int(raw_input("How many stones would you like to take?"))
        except ValueError:
            print "Please enter a number"
            # Start over
            continue
        
        if(piles[pile-1] >= stones):
            piles[pile-1] -= stones
            break
        else:
            print "You entered an invalid amount of stones."

    showPiles()

    if piles[0] == 0 and piles[1] == 0 and piles[2] == 0:
        # game over
        if turn == ONE:
            # player one took the last stone, player 2 wins
            print "Player two wins!"
        else:
            # player two took the last stone, player 1 wins
            print "Player one wins"
    else:
        # change the player
        if turn == ONE:
            turn = TWO
        else:
            turn = ONE

    print


Was This Post Helpful? 1
  • +
  • -

#73 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Week #4 Challenge: Python

Posted 27 January 2010 - 02:50 PM

Also, just some suggestions for people who want to write something in Python but don't know what to write: Quick Sort implementation or Binary Search implementation.
Was This Post Helpful? 0
  • +
  • -

#74 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 19
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Re: Week #4 Challenge: Python

Posted 27 January 2010 - 02:53 PM

Thanks! Yea, I knew about using false and true... I was trying this:

while a != false:



and Netbeans was giving me some problems. Thanks for that! Also, the exception catching stuff is useful. Thanks! I haven't figured out how to use exceptions yet. thanks :)
Was This Post Helpful? 0
  • +
  • -

#75 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Week #4 Challenge: Python

Posted 27 January 2010 - 02:56 PM

I think in Py3 you would have to do it like this:

if nGuess in guesses:
		print("You already guessed " + str(nGuess))


Sort of sucks that they are breaking backwards compatibility...should just throw a deprecated flag instead of breaking old code. Is there a flag that will make the Py3 interpreter run 2.6 code?

This post has been edited by SpeedisaVirus: 27 January 2010 - 02:58 PM

Was This Post Helpful? 1
  • +
  • -

  • (8 Pages)
  • +
  • « First
  • 3
  • 4
  • 5
  • 6
  • 7
  • Last »