PDA

View Full Version : Number Game


EarlVarsen
01-07-2004, 05:57 PM
This is kind of a newbie question, but oh well, I started 4 days ago.

How would I make a "number game" where it randomly picks a number, 1 - 100, i know how ro make it where if you guess too high, it'll say "guess lower" and all that. How do I make it randomly pick a number 1 - 100?

Also, not a must, but it would be cool to tell them how many tries it took them to get it right, how would I do that?

Oh yeah, I'm using Python.

BleuIce
01-09-2004, 12:59 AM
I did this in Java not long ago as a school project. Same thing exactly. If you want the Java code to think it through, I can post it up. I don't know much about python though.

SolarBear
01-11-2004, 01:15 PM
I'm writing this without even checking if it's right, so it gotta work ;)
import random

#choose a random number from 1 to 100
number = random.randint(1,100)
guess = 0
tries = 0

while number != guess:
tries += 1
guess = input("Try to guess the number !")
if guess > number:
print 'Lower!'
elif guess < number :
print 'Higher!'
else:
print 'Right you are, dude!'
brea

print 'You needed ',tries,' to find it. Good job!'

Not fancy, really, but does the job.

EarlVarsen
01-11-2004, 11:34 PM
Thanks. I didn't know how to make it pick a random integer, i just did random.random... I also didn't know how to make it print how many tries you took. Could you explain what the += means, and why it equals 1? Thanks!

SolarBear
01-12-2004, 12:27 AM
These operators are nothing but shortcuts to us lazy coders. 8)
#The following 2 operations are the same
a = a + 2
a += 2
All +=, -=, *=, /=, %= and the like do an operation on the left value. So a += 2 is 'take a, add 2 to its value and put that value in a'.

So in our code, I initialize tries to 0 and at the beginning of each loop, we add 1 to the number of tries. Simple as that !

kryptech.net
02-16-2004, 06:53 PM
god come on guys, you need something like this(C++/C)
int number = rand() % 100;
int lastans;
int tries;
while ( lastans != number )
{
std::cout << "\nGuss my number(1-100): ";
std::cin >> lastans;
tries++;
}
std::cout << "\n\nTHATS IT!!! You gussed it, my number was" << number << ". It took you " << tries << " times to guess it!";
taht's all you better be using a loop, cuz thats the only easy way you are going to get that number of tries thing to work!!!

Strike
02-16-2004, 07:06 PM
kryptech.net: you forgot to seed the random number generator.

kryptech.net
02-17-2004, 09:17 AM
How do you do that I alaways have my seed's messed up so each time I run I get the same repetitive set of numbers...