can someone tell me why this doesn't work please python 3
def ask_ok(prompt, retries=4, complaint="Yes or no, please!"):
while True:
password = input("enter something")
if password in ('y', 'ye', 'yes'): return True
if password in ('n', 'no', 'nope'): return False
retries = retries - 1
if retries < 0:
raise IOError('refusenik user')
print(complaint)--
http://mail.python.org/mailman/listinfo/python-list
is there a shorter way to write this
I had a task in a book to pick 5 items from a list of 26 ensuring the items are not repeated import random list = ['a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z'] word = ' ' a = random.choice(list) list.remove(a) b = random.choice(list) list.remove(b) c = random.choice(list) list.remove(c) d = random.choice(list) list.remove(d) e = random.choice(list) list.remove(e) word = a + b + c + d + e print (word) print(list) -- http://mail.python.org/mailman/listinfo/python-list
python 3 error i know the file works in python 2.6
can someone help me please
#open file and read last names
filename = input('name file')
file = open(filename, 'r')
names_list = file.readlines()
file.close()
#open a file for saving passwords
outfile_name = input('Save passwords')
outfile = open(outfile_name, 'a')
#create a password for each name in list
import random, string
name = ('')
for name in names_list:
name_prefix = name[0:2]
number = random.randrange(100,999)
name_prefix = str.upper(name_prefix)
password = name_prefix + str(number)
whole_line = (password)
print (password)
outfile_name.write(whole_line)
outfile_name.close()
print (password)
error
Traceback (most recent call last):
File "C:\Documents and Settings\Gary\Desktop\python\bembry\pa2.i.py", line
21, in
outfile_name.write(whole_line)
AttributeError: 'str' object has no attribute 'write'--
http://mail.python.org/mailman/listinfo/python-list
just got the g1
Hi Just got the G1, is their any way to get python running on the andriod platform ? -- http://mail.python.org/mailman/listinfo/python-list
hi can someone help me i would like to run this program 3 times and would like to append the cPickle file as a high score table
hi,
Can someone help me i would like to run this program 3 times or more and would
like to append the cPickle file as a high score table keeping my top scores.
Right now it only records the last score thanks.
# Trivia Challenge
# Trivia game that reads a plain text file
def open_file(file_name, mode):
"""Open a file."""
try:
the_file = open(file_name, mode)
except(IOError), e:
print "Unable to open the file", file_name, "Ending program.\n", e
raw_input("\n\nPress the enter key to exit.")
sys.exit()
else:
return the_file
def next_line(the_file):
"""Return next line from the trivia file, formatted."""
line = the_file.readline()
line = line.replace("/", "\n")
return line
def next_block(the_file):
"""Return the next block of data from the trivia file."""
category = next_line(the_file)
question = next_line(the_file)
answers = []
for i in range(4):
answers.append(next_line(the_file))
correct = next_line(the_file)
if correct:
correct = correct[0]
explanation = next_line(the_file)
return category, question, answers, correct, explanation
def welcome(title):
"""Welcome the player and get his/her name."""
print "\t\tWelcome to Trivia Challenge!\n"
print "\t\t", title, "\n"
def main():
trivia_file = open_file("trivia.txt", "r")
title = next_line(trivia_file)
welcome(title)
score = 0
bonus = 0
tries = 0
# get first block
category, question, answers, correct, explanation = next_block(trivia_file)
while category:
# ask a question
print category
print question
for i in range(4):
print "\t", i + 1, "-", answers[i]
# get answer
answer = raw_input("What's your answer?: ")
tries = tries + 1
# check answer
if answer == correct:
print "\nRight!",
score += 1
if tries == 1:
bonus = 5
elif tries == 2:
bonus = bonus + 10
elif tries == 3:
bonus = bonus + 20
elif tries == 4:
bonus = bonus + 30
elif tries == 5:
bonus = bonus + 40
else:
bonus = bonus
print "\nWrong.",
print explanation
print "Score:", score, "\n\n"
# get next block
category, question, answers, correct, explanation =
next_block(trivia_file)
trivia_file.close()
print "That was the last question!"
print "You're score is:", score, "and your bonus", bonus
total = score + bonus
print "for a grand total ", total
import cPickle, shelve
name = raw_input("what is your name")
High_Score = [name, total]
pickle_file = open("pickles5.dat", "w")
cPickle.dump(High_Score, pickle_file)
pickle_file.close()
# to read the pickle_file
pickle_file = open("pickles5.dat", "r")
High_Score = cPickle.load(pickle_file)
print High_Score, "High Scores"
main()
raw_input("\n\nPress the enter key to exit.")
--
http://mail.python.org/mailman/listinfo/python-list
can someone with guessing a number
I would just like the program to exit after guessing the amount of numbers
wrong
# Guess My Number
import random
the_number = random.randrange(100) + 1
tries = 1
# guessing loop
while (guess != the_number):
if (guess > the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take a guess: "))
tries += 1
if tries > 10:
print 'you failed- give up'
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
raw_input("\n\nPress the enter key to exit.")
many Thanks --
http://mail.python.org/mailman/listinfo/python-list
for some reason the actual tries figure is not right
can someone explain why the tries displays the wrong number
thanks
orginally i started off with tries = 0 but it was off by 2
# Word Jumble
#
# The computer picks a random word
# The player has to guess the original word can give hint
import random
tries = 1
# create a sequence of words to choose from
word = ("python", "jumble", "xylophone", "difficult", "answer")
# pick one word randomly from the sequence
word = random.choice(word)
# create a variable to use later to see if the guess is correct
correct = word
# start the game
print \
"""
Welcome to Word Jumble!
(Press the enter key at the prompt to quit.)
"""
if word == "python":
hint = 'snake'
if word == "jumble":
hint = 'mess'
if word == "difficult":
hint = 'hard'
if word == "answer":
hint = 'correct'
if word == "xylophone":
hint = 'music'
guess = raw_input("\nYour guess: ")
guess = guess.lower()
while (guess != correct) and (guess != ""):
print "Sorry, that's not it." + hint
guess = raw_input("Your guess: ")
guess = guess.lower()
if guess != correct:
print hint
tries += 1
if guess == correct:
print "That's it! You guessed it!\n"
print "Thanks for playing."
print tries
raw_input("\n\nPress the enter key to exit.")
--
http://mail.python.org/mailman/listinfo/python-list
[no subject]
Hi there. So I have a challenge in the Python book I am using (python programming for the absolute beginner) that tells me to improve an ask_number() function, so that it can be called with a step value, and I havn't been able to find out yet what's meant by a step value, but i'll keep looking of course. I'd just be grateful if someone could illimunate this for me. def ask_number(question, low, high):"""Ask for a number within a range.""" response = Nonewhile response not in range(low, high):response = int(raw_input(question))return response Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list
very newbie question
stuck on python for absolute beginners
chapter 6
i actually done what i was supposed to do use the function ask_number for guess
a number
but for some reason it does not count correctly the number of tries
# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money
import random
print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"
# set the initial values
def ask_number():
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1
while (guess != the_number):
if (guess > the_number):
print "Lower..."
else:
print "Higher..."
tries += 1
guess = int(raw_input("Take a guess: "))
tries += 1
ask_number()
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
raw_input("\n\nPress the enter key to exit.")
--
http://mail.python.org/mailman/listinfo/python-list
has anyone completed python for the absolute beginner ?
has anyone completed python for the absolute beginner ? chapter 7 in the book the trivia_challenge.py file does not work correctly and does not display all the correct questions - and even though the answer is correct it will still state the answer is wrong. this file is from the CD i have not adjusted it in any way?? -- http://mail.python.org/mailman/listinfo/python-list
can someone explain why this happens- newbie question
Hi
can someone tell me why it prints the high score table multiple times?
#high scores program
scores =[]
choice = None
while choice != 0:
print """high Score Table
0 - exit
1 - show Scores
2 - add a score
3 - delete a score
4 - sort scores
"""
choice = input("what would you like to do?")
if choice == 0:
print "goodbye"
elif choice == 1:
for score in scores:
print scores
elif choice == 2:
score = input("add a score")
scores.append(score)
elif choice == 3:
score = input("what score would you like to delete ?")
if score in scores:
scores.remove(score)
else:
print "that score is not listed"
elif choice == 4:
scores.sort()
scores.reverse()
print scores, "highest score first"
--
http://mail.python.org/mailman/listinfo/python-list
