Question:
Python programming question.?
nerve34
2010-01-16 21:04:41 UTC
Listing_1-2.py
# Copyright Warren Sande, 2009
# Released under MIT license http://www.opensource.org/licenses/mit-license.php
# Version 61 ----------------------------

# Number Guess game

import random

secret = random.randint(1, 99) # pick a secret number
guess = 0
tries = 0

print "AHOY! I'm the Dread Pirate Roberts, and I have a secret!"
print "It is a number from 1 to 99. I'll give you 6 tries. "

# try until they guess it or run out of turns
while guess != secret and tries < 6:
guess = input("What's yer guess? ") # get the player's guess
if guess < secret:
print "Too low, ye scurvy dog!"
elif guess > secret:
print "Too high, landlubber!"
tries = tries + 1 # used up one try

# print message at end of game
if guess == secret:
print "Avast! Ye got it! Found my secret, ye did!"
else:
print "No more guesses! Better luck next time, matey!"
print "The secret number was", secret

I just got a book about learning python,and i enter this exact code from the book,and i either get errors or the guessing game only gives me 2-4 guesses instead of the 6 it is supposed to give me.
This is really frustrating me.I can't figure out what i am doing wrong.Something stupid i am sure.

I got python 2.5 and 2.6 installed could that be the problem?
Three answers:
Yahoo! Answerer
2010-01-16 21:31:05 UTC
You need to tab for your blocks.

And there is an issue with the:



if guess == secret:

print "Avast! Ye got it! Found my secret, ye did!"

else:

print "No more guesses! Better luck next time, matey!"

print "The secret number was", secret







I tweaked the code a bit and it works fine for me:



# Copyright Warren Sande, 2009

# Released under MIT license http://www.opensource.org/licenses/mit-l…

# Version 61 ----------------------------



# Number Guess game



import random



secret = random.randint(1, 99) # pick a secret number

guess = 0

tries = 0



print "AHOY! I'm the Dread Pirate Roberts, and I have a secret!"

print "It is a number from 1 to 99. I'll give you 6 tries. "

print ""



# try until they guess it or run out of turns

while guess != secret and tries < 6:

guess = input("What's yer guess? ") # get the player's guess

if guess < secret:

print "Too low, ye scurvy dog!"

print ""

elif guess > secret:

print "Too high, landlubber!"

print ""

tries = tries + 1 # used up one try

if guess == secret:

print "Avast! Ye got it! Found my secret, ye did!"

print ""



if tries >= 6:

print "No more guesses! Better luck next time, matey!"

print "The secret number was", secret

print ""

else:

print "Congratulations!"















just make sure that you tab over for if and while blocks, if you send me a private message, i can send you the file with the exact formatting.
2010-01-16 21:16:04 UTC
First, make sure your code is formatted correctly. since I can't see the formatting, I'm going to take a wild guess and say that "tries = tries + 1" isn't in the right spot. It should be lined up with "guess = input("What's yer guess? ") " Also, judging from the fact that the book uses print and not print(), I'm guessing that the book does not use 3.0.
?
2016-05-26 10:24:36 UTC
If you have read anything about sound in "Console-Mode" Python, that's because there is no sound feature with Console Python. You need to learn wxPython or PyGame or maybe even Tkinter (I think supports sound features)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...