# Python: Guess my number 1-100

Page 1 of 1

## 5 Replies - 40912 Views - Last Post: 21 March 2011 - 01:11 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=221212&amp;s=a0d0aa5254274d75de8ebaf4f0081502&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hottiechick426

Reputation: 0
• Posts: 2
• Joined: 10-March 11

# Python: Guess my number 1-100

Posted 10 March 2011 - 12:39 AM

So I just need some help. I don't want you all to do my homework for me. This is the assignment:

This program should generate a random number between 1 and 100 inclusive. It then asks the player to guess the
number. If it is not correct, the program says whether the user’s number is greater than or less than the actual number,
and asks the player to guess again. When the player guesses the number, the program says so and prints out the number
of guesses needed.

This is my brainstorming thoughts:
```print("Guess a number between 1 and 100")
line=input("Guess a number between 1 and 100")
count=0
count=count + 1

print("The number is between 1 and 100")
print("That's right! You got it in"count "guesses")
```

Any suggestions or tips or help for me to do next?

Is This A Good Question/Topic? 0

## Replies To: Python: Guess my number 1-100

### #2 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Python: Guess my number 1-100

Posted 10 March 2011 - 01:30 AM

You will need to look into constructs such as the while loop. That will allow you to more easily do the same thing multiple times.

### #3 hottiechick426

Reputation: 0
• Posts: 2
• Joined: 10-March 11

## Re: Python: Guess my number 1-100

Posted 10 March 2011 - 01:32 AM

```print("Guess a number between 1-100")
counts = 0
counts = counts +1
max = 101
min = 1

guess = (max-min)/2

while guess !=number:
if number>guess:
min=guess
elif number<guess:
max =guess
else

guess =(max-min)/2
print("That right! You got", counts, "count.")

```

This is my progress thus far using the while loop. More help please?

• Saucy!

Reputation: 6245
• Posts: 24,013
• Joined: 23-August 08

## Re: Python: Guess my number 1-100

Posted 10 March 2011 - 06:28 AM

Uh...don't you think you should read a number in before you go about trying to guess it?

### #5 e-papa

Reputation: 4
• Posts: 45
• Joined: 17-March 11

## Re: Python: Guess my number 1-100

Posted 21 March 2011 - 02:59 AM

First you have to import the random module into your python IDE, using this
```import random
```
then you have to tell your program to generate a number between 1 and 100 to do that all use the function randint() in the random module, for example
```import random
random.randint(1,100)
```
this simply allows the program to generate a random integer between 1 and 100, 1 and 100 inclusive, then i believe you can assign a value to a variable, you assign the value from function to a variable like this
```import random
number=randint(1,100)

```
then you have to to atart a counter
```attempts=0
```
after which you start your while loop it is in this while loop you take input from the keyboard
```while answer != number:
attempts = attempts + 1
answer = input( 'Attempt ' )
```
this counts the number of attempts until the answer equals the number, then we need a conditional statement for the higher or lower part, and the else for if the answer is correct
```if answer > number :
print ('Lower')
print ('Higher')
print ('You got it in %i tries!' % (attempts))
```

```print('the number is', number)
```

together we have
```import random
number = random.randint( 1, 100 )
print ('I\'m thinking of a number between 1 and 100')
print ('What is the number I\'m thinking of?')
attempts = 0
attempts = attempts + 1
answer = input( 'Attempt ' )
print ('Lower')
print ('Higher')
print ('You got it in %i tries!' % (attempts))
print('the number is', number)
```

### #6 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• Joined: 23-December 08

## Re: Python: Guess my number 1-100

Posted 21 March 2011 - 01:11 PM

e-papa in the future, please don't write out the entire answer to a problem when the original poster says "I don't want you all to do my homework for me" as this takes away the poster's opportunity to work through the problem and find a solution.

This post has been edited by atraub: 21 March 2011 - 01:12 PM