I wonder if you actually ran your program.
I might be mistaken as I have yet to code any Python code. However, at a first glance I appear to find two problem areas:
The if statement just in front of the while does not return a value if the condition is met. Also, it is perfectly redundant since the while statement would fall through in that case anyway.
Also, you might want to check the placement of the statements which read the next guess within the while. I would think that your program outputs a wrong count if you enter first a low and then a high guess.
The structure of your program is very clean and easy to follow. I would not have been able to spot those two issues at a glance if that was not the case. You'd get points on that count, anyway.
Aswell better late than never, here is my entry using C++, which i've just started.
Code:#include <iostream> #include <cstdlib> #include <ctime> using namespace std; void startguess(int num); int main() { srand((unsigned)time(0)); startguess((rand()%100)+1); return 0; } void startguess(int num) { int guess=0, guesses=0; while(guess != num && guesses++ != -1){ cout << "Guess the number, 1-100: "; cin >> guess; if(guess > num) cout << "Not so high!\n"; else if(guess < num) cout << "That's too low!\n"; } cout << "You're damn right. It took you " << guesses << " guesses.\n"; }
Last edited by cybermacro; September 30th, 2008 at 11:35 AM.
I suppose I am involving in necromancy now. But Here We Go then:
I must mention I did not know how to randomize and I took the idea from cardboardtoast. I guess random.choice(range(1,101) will randomly give a number b/w 1 and 100 and its second argument is exclusive.Code:#!/usr/bin/python print"""################# # Guessing Game # #################""", '\n\n' import random numGuess = 0 tries = 0 RandomNum = random.choice(range(1,101)) print "Random Number Generated\n\n" while True: numGuess = input("Enter the number which you think the computer has selected: ") tries+=1 if numGuess == RandomNum: print '\nCongratulations. You selected the right number' break elif numGuess > RandomNum: print '\nYou guessed higher than the number actually is...' else: print '\nYou guessed lower than than the number actually is...' print "You guessed the number", RandomNum, " in ", tries, " attempts."
BTW It's interesting that we don't need to randomize the timer. Cool.
python has random.randint(1,100).
Here's the official module documentation index: http://docs.python.org/modindex.html
À la chasse au boson intermédiaire
i love scheme
PHP Code:
(define (getmax)
(begin
(flush-output)
(display "hello, pick a number that will be top of the guessing range\n")
(flush-output)
(newline)
(read)))
(define (check number)
(define input 0)
(display "what is your guess?")
(newline)
(flush-output)
(set! input (read))
(cond ((< input number) (display "too low!") (newline) (check number))
((= input number) (display "you win!") (newline))
((> input number) (display "too high!") (newline) (check number))))
(define (game) (define input 0) (check (random (getmax))) (display "play again"))
;this is a comment - this part simply calls the game method
(begin (game))
Using python3 .
PHP Code:
#!/usr/bin/python3
import random
target = random.randint(1,100)
test = -1
attempts = 0
def check_int(user_num):
''' Verify that the input is an integer'''
try:
user_num = int(user_num)
return True
except TypeError:
print(' Please input an integer.')
return False
except ValueError:
print(' Please input an integer.')
return False
def check_range(user_num):
''' Verify that the input is between 1 and 100'''
good_guess = True
if user_num <= 0:
print(' Please input an integer between 1 and 100.')
good_guess = False
if user_num > 100:
print(' Please input an integer between 1 and 100.')
good_guess = False
return good_guess
def check_guess(user_num):
'''Test the user guess and provide feed back'''
if check_range(user_num):
if user_num < target:
print('Guess Higher:')
if user_num > target:
print('Guess Lower:')
if user_num == target:
print(' Congrats you guessed the correct number ' + str(target) +'.')
if attempts > 1:
print(' It took you ' + str(attempts) +' attempts.')
else:
print( 'It took you one try!')
return user_num
# The actual game takes place below.
print(' In this game you must guess the randomly selected number between 1 and 100.')
print(' Only input integers between 1 and 100.\n The game will stop after you \
have made a correct guess.\n \n Make a guess:')
while test != target:
guess = input()
attempts = attempts + 1
if check_int(guess):
test = check_guess(int(guess))
Better late than never I suppose, did it in C.
Use "gcc game.c -o game", then "./game" to run
PHP Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** agv)
{
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
int correctNum = rand() % (100 - 1);
int uinput;
int tries;
printf("\nYour objective is to guess a random number between 1 and 100.\n(our seed is %d by the way)\n\n", seconds);
for(tries=1;tries <= 10;tries++)
{
printf("Enter a number: ");
scanf("%d", &uinput);
if (uinput < correctNum) printf("too low, %d guesses left\n\n", (10 - tries));
if (uinput > correctNum) printf("too high, %d guesses left\n\n", (10 - tries));
if (uinput == correctNum)
{
printf("Correct! You used %d guesses\n", tries);
return 0;
}
}
printf("You fail! The number was %d!", correctNum);
return 0;
}
Last edited by module0000; January 19th, 2009 at 03:59 AM. Reason: turned on syntax highlighting
A bit of irony: it looks like LaRosa wants you to write code to make the USER do the binary search! <chuckle>
Comments/Advice Welcome
PHP Code:
#!/usr/bin/python
import random
guess='0'
tries=0
num=random.randrange(1,100)
#the whole guessing part
while guess != 'quit' and int(guess) != num:
guess=raw_input("Try to guess the number: ")
if int(guess) > num:
print "Too high"
if int(guess) < num:
print "Too low"
tries=tries+1
#if they get it
if guess != 'quit':
print "\nYou got it in %i tries\n" % (tries)
$(fortune)
In a world without walls and fences, who needs windows and gates?
Bookmarks