I'm able to create a guessing number game, but I can limit the amount of guesses to less than 3. Can someone tell me what i'm doing wrong? - # Guess My Number
-
# The computer picks a random number
-
-
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
-
the_number = random.randrange(100) + 1
-
guess = int(raw_input("Take a guess: "))
-
tries = 1
-
-
# guessing loop
-
-
while (guess != the_number):
-
if (guess > the_number):
-
print "Lower..."
-
else:
-
print "Higher..."
-
-
guess = int(raw_input("Take a guess: "))
-
if tries > 3:
-
print "You have more than 3 attempts, game over."
-
tries += 1
-
-
else:
-
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.")
7 16784
Please wrap your code in CODE tags so that the indentation doesn't get stripped out.
It looks like you only update tries when it's greater than three. Your idea would work, but you also can't have
because Python won't know to associate the else with the if (can't tell since the parser here rips indentation out). Put the tries update someplace else to fix that.
bvdet 2,851
Expert Mod 2GB
I'm able to create a guessing number game, but I can limit the amount of guesses to less than 3. Can someone tell me what i'm doing wrong? -
# Guess My Number
-
# The computer picks a random number
-
-
-
-
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
-
the_number = random.randrange(100) + 1
-
guess = int(raw_input("Take a guess: "))
-
tries = 1
-
-
# guessing loop
-
-
while (guess != the_number):
-
if (guess > the_number):
-
print "Lower..."
-
else:
-
print "Higher..."
-
-
guess = int(raw_input("Take a guess: "))
-
if tries > 3:
-
print "You have more than 3 attempts, game over."
-
tries += 1
-
-
else:
-
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.")
I made a few changes to your code. The statement to get guess is now inside the loop. Variable tries is incremented after the guess. The if statement that checks for high or low was modified: if >, elif<, else(must be correct guess). If tries == max_guesses, the game is over. Encapsulate the code in a function. Use return to exit the while loop. - 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"
-
-
def guess_num(num, max_guesses):
-
# guessing loop
-
tries = 0
-
while True:
-
guess = int(raw_input("Take a guess: "))
-
tries += 1
-
if guess > num:
-
print "Lower..."
-
elif guess < num:
-
print "Higher..."
-
else:
-
print "You guessed it! The number was", num
-
print "And it only took you %d tr%s!\n" % (tries, ['ies', 'y'][tries==1 or 0])
-
return
-
-
if tries == max_guesses:
-
print "You have had %d guesses. Game over." % max_guesses
-
return
-
-
the_number = random.randrange(1,101)
-
max_guesses = 3
-
guess_num(the_number, max_guesses)
-
-
raw_input ("\n\nPress the enter key to exit.")
Thanks bvdet. The book "Python Programming second edition hasn't shown the "def" command yet.
The original exercise - Modify this program so the player has a limited number of guesses. The chp covers import random, randomrange, while loop, while true, if, if-else loop and if elif loop. Is there a way I can do with the def command?
The original exercise modify this program to limit the amount of guesses.
Chapter covers if, if-else, if-elif, if-elif-else, while, while True, import random, randrange(), raw_input and comparison operators. How can I do it without using the def command.
Original exercise that I have to modify listed below. - # Guess My Number
-
-
# The computer picks a random number
-
# 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
-
# the_number = random.randrange(100) + 1
-
# guess = int(raw_input("Take a guess: "))
-
# 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
-
-
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.")
bvdet 2,851
Expert Mod 2GB
Could you clarify what your problem or question is?
Could you clarify what your problem or question is?
dseto's programming course hasn't covered functions yet.
The original exercise modify this program to limit the amount of guesses.
Just modify the condition in the while-loop so that the loop also stops when the number of tries is too high. You'll need the and operator.
Hi, I've taken a look at your code and your on the right track. Remember loops are an extremely important part of any program especially one like this. Below is a working guess the number game that requires you to input the number of max tries when you call the function. -
#! /usr/bin/env python
-
-
# Guess the number game
-
-
import random
-
-
def main(max):
-
print "\tWelcome to 'Guess my number'!"
-
print "\nI'm thinking of a number between 1 and 100."
-
print "Try to guess in as few attempts as possible.\n"
-
num = random.randrange(101)
-
tries = 0
-
guess = ""
-
while guess != num:
-
guess = int(raw_input("Take a guess: "))
-
if guess > num:
-
print "Too high."
-
elif guess < num:
-
print "Too low."
-
tries += 1
-
if tries >= max:
-
print "Sorry, you took too many guesses. Game Over"
-
exit()
-
print "Congratulations!"
-
again = raw_input("To play again press Enter. Type anything else to quit.")
-
if again == "":
-
main()
-
else:
-
exit()
-
-
main(3)
-
The business part of this code is lines 14-23. thats the important while loop that keeps the game going. As you can see it will keep iterating while the variable 'guess' does not equal variable 'num' (while guess != num:)
This code doesnt have any try-except statements or any error checking for user input or anything like that but it is working. If you need any additional explanation please post again. Thanks
Bodsda
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Irmen de Jong |
last post by:
Hello, some trivia for whom it may concern:
I've just downloaded and played the demo of Cyan's new
game "URU Ages of Myst". Wonderful-game-but-crappy-controls
yadda yadda, but that's not the...
|
by: Moosebumps |
last post by:
When I have time, I am planning to evaluate Python for console game
development (on Playstation 2, GameCube, and Xbox). Does anyone have any
experience with this?
Pretty much the only resource...
|
by: EAS |
last post by:
Hey, I'm new to python (and programming in general) so I'll prolly be around
here a lot...
Anyways, I've found out how to make a "guess my number game" where the
player guesses a number between...
|
by: chyf1983929 |
last post by:
Is there any modules about mobile game in python? which l can use it to write mobile game .
thank you
|
by: Yannick |
last post by:
Hi,
I would like to program a small game in Python, kind of like robocode
(http://robocode.sourceforge.net/).
Problem is that I would have to share the CPU between all the robots,
and thus...
|
by: blinkrebel |
last post by:
Hello
I need some help implementing the game of blackjack using the xturtle package.
the instructions can be found at
http://katie.luther.edu/moodle/file.php/2387/BlackJack.pdf
and .gif's...
|
by: Dew |
last post by:
Hello, iam new to programming, and ive been given a task in my Uni to complete certain exercises but i found it very difficulty to solve them. I was wandering if there is someone who can help me...
|
by: cromoglic |
last post by:
Hello! I have been making a "guess what number the computer thinks of"-game in python (text-only) for educational reasons. Now, i want to implement a highscore in the code, which writes the best...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |