Results 1 to 4 of 4
Im am trying to create a program where I type a number between 1 and 100 and the computer randomly guesses numbers till it gets the correct number I chose. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 06-22-2006 #1Just Joined!
- Join Date
- Mar 2006
- Posts
- 32
Troubles with the while statement
Im am trying to create a program where I type a number between 1 and 100 and the computer randomly guesses numbers till it gets the correct number I chose. I think I have the general concept of this code working, but there are a few things that aren't happening that I want to happen. One of them is that after typing a number that is too big or too small, it ends the program. I need it to continue asking for a number untill a correct number is typed. I've tried placing while around the ifs that tell you if your number is correct, so it can loop, but no such luck. Also, what would I have to do in order for the program to ask for a number again even if I put in a letter or string.
Code:# this time, the computer has to guess the number # By: Justin # Imports the random number generator import random # Prints the title of the program print "\n\n\t\tWelcome to the \"Stump the Computer!\" game!" print "\t\tThe game where YOU stump the computer!\n" print "\t\t\tBy: Justin" print "\t\t\t \\ \\ \\ \\ \\\n\n" # Inputing the number the_number = "" while not the_number: the_number = raw_input("Type number: ") the_number = int(the_number) # Checks to see if the number is reasonable if the_number < 1: print "\aSorry that number is too small, try again" elif the_number > 100: print "\aSorry that number is too big" else: print "Okay"
- 06-22-2006 #2
As far as checking the input's range, I would do something like this:
As far as checking whether or not it's an integer, you can use a regular expression, iterate over the characters in the input string to check whether or not it contains a non-digit, or you could probably use Python's exception handling mechanism; I'm sure it includes bad conversion exceptions.Code:input = -1 while((input < 1) and (input > 100)): input = raw_input('Type number (1-100): ') input = int(input) # Do stuff now!Flies of a particular kind, i.e. time-flies, are fond of an arrow.
Registered Linux User #408794
- 06-22-2006 #3Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,199
Hi.
Small typo; probably should be:
Originally Posted by Javasnob
cheers, drlCode:while((input < 1) or (input > 100)):
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 06-22-2006 #4
Whoops...thanks for catching that...
Flies of a particular kind, i.e. time-flies, are fond of an arrow.
Registered Linux User #408794


Reply With Quote
