108 Replies - 15910 Views - Last Post: 06 September 2011 - 12:05 PM
#61
Re: Week #4 Challenge: Python
Posted 26 January 2010 - 04:03 PM
#62
Re: Week #4 Challenge: Python
Posted 26 January 2010 - 07:25 PM
#63
Re: Week #4 Challenge: Python
Posted 26 January 2010 - 08:15 PM
programble, 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
#64
Re: Week #4 Challenge: Python
Posted 27 January 2010 - 06:41 AM
Quote
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.
#65
Re: Week #4 Challenge: Python
Posted 27 January 2010 - 07:36 AM
#66
Re: Week #4 Challenge: Python
Posted 27 January 2010 - 09:28 AM
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!")
#67
Re: Week #4 Challenge: Python
Posted 27 January 2010 - 12:31 PM
#68
Re: Week #4 Challenge: Python
Posted 27 January 2010 - 12:54 PM
#69
Re: Week #4 Challenge: Python
Posted 27 January 2010 - 01:54 PM
chili5, on 27 Jan, 2010 - 11:31 AM, said:
Good idea! Thank you man
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!")
#70
Re: Week #4 Challenge: Python
Posted 27 January 2010 - 02:09 PM
#71
Re: Week #4 Challenge: Python
Posted 27 January 2010 - 02:27 PM
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?
#72
Re: Week #4 Challenge: Python
Posted 27 January 2010 - 02:42 PM
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
#73
Re: Week #4 Challenge: Python
Posted 27 January 2010 - 02:50 PM
#74
Re: Week #4 Challenge: Python
Posted 27 January 2010 - 02:53 PM
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
#75
Re: Week #4 Challenge: Python
Posted 27 January 2010 - 02:56 PM
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
|
|

New Topic/Question
Reply




MultiQuote








|