5 Replies - 21024 Views - Last Post: 21 March 2011 - 01:11 PM Rate Topic: -----

#1 hottiechick426  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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("Your guess is too high")
Print("Your guess is too low")
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  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 hottiechick426  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
          print'Your number is' +str(guess)

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



This is my progress thus far using the while loop. More help please?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,547
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 e-papa  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • 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 ' )
  answer = int( answer )
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')
  elif answer < number :
    print ('Higher')
  elif answer == number :
    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
while answer != number:
  attempts = attempts + 1
  answer = input( 'Attempt ' )
  answer = int( answer )
  if answer > number :
    print ('Lower')
  elif answer < number :
    print ('Higher')
  elif answer == number :
    print ('You got it in %i tries!' % (attempts))
print('the number is', number)

Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • 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

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1