I need help with the function but i have no clue how to?.. the function should generate a random number between 1 and 100 and then allow the user to guess the number. After each guess it should display if the guess is too high or low and if they guess it in 7 chances then it should say you win or if they dnt it should say you loose....

import random
number=random.randint(1,100)
guess=input("please enter your guess: ")
if guess<number:
    print "too low"
if guess>number:
    print"too high"

Recommended Answers

All 7 Replies

Try this:

import random

number=random.randint(1,100)
victory = False

for x in range(0,7):
    guess=input("please enter your guess: ")
    if guess<number:
        print "too low"
    if guess>number:
        print"too high"
    if guess == number:
        print "Correct!"
        victory = True

if victory == False: #If they didn't guess the number by the end...
    print "Sorry, you didn't guess the number."

If you need any more help, feel free to post back.

Try this:

import random

number=random.randint(1,100)
victory = False

for x in range(0,7):
    guess=input("please enter your guess: ")
    if guess<number:
        print "too low"
    if guess>number:
        print"too high"
    if guess == number:
        print "Correct!"
        victory = True

if victory == False: #If they didn't guess the number by the end...
    print "Sorry, you didn't guess the number."

If you need any more help, feel free to post back.

whts victory and it being ture and false? and whts this line do "for x in range(0,7):" ? and is there any way of displaying the real number?

thx

hehe, i kinda got carried away..

# import all of our needed random functions
import random

won = False

def guess_7_times():
     global won
     print('Welcome to the number guessing game!')
     # our random number
     real_number = random.randint(1,100)
     # how may guesses we have until we loop out of the function and exit
     guesses = 7

     # a loop; it will keep runing as long as guesses > 1
     while guesses > 0:
          guess = input('Guess a number between 1 and 100! ')
          # if our guess is too big
          if guess > real_number:
               print('Your guess is to big!\n')
          # if our number is less than real_number..
          elif guess < real_number:
               print ('Your number is too small!\n')
          # or, if our guess IS equal to real_number
          elif guess == real_number:
               print('Woot! You guessed the correct number!!')
               # print you got the correct number. if your wondering what
               # +str(real_number) means, all we are doing is converting our
               # number, real_number, into a string that python can print.
               # for some reason, python wont let you combine strings and numbers
               # when printing text, so we have to convert it into a string.
               print('The correct number was '+str(real_number))
               won = True
               # 'break' out of our loop
               break

          
          # minus guesses by 1 each loop
          # note: guesses -= 1 is the same as saying
          # guesses = guesses-1
          guesses -= 1
          
         
# let's call our function!
guess_7_times()

if won == False:
     print('you lost....')
else:
     pass

BTW http://en.wikibooks.org/wiki/Python_Programming/Functions is a great page for learning about functions. I would check it out!

"victory" is a variable that is set to "True" if the user guesses the number or "False" if they do not. At the end of the script, the last two lines check what "victory" is. If it is False (they have not guessed the number), it prints that statement.

If you want the script to also tell you what the number was, add this line right after "print "Sorry, you didn't guess the number."".

print "The number was", number

As for the line "for x in range(0,7)"...think of it this way. For every number between 0 and 7 (there are 7 of them)....it will call the following code. So: it calls that part of the code (called a block) seven times!

"victory" is a variable that is set to "True" if the user guesses the number or "False" if they do not. At the end of the script, the last two lines check what "victory" is. If it is False (they have not guessed the number), it prints that statement.

If you want the script to also tell you what the number was, add this line right after "print "Sorry, you didn't guess the number."".

print "The number was", number

As for the line "for x in range(0,7)"...think of it this way. For every number between 0 and 7 (there are 7 of them)....it will call the following code. So: it calls that part of the code (called a block) seven times!

this is my current code but it doesn't seem to print the randomnmber chosen?.. i get a error saying " print "The number was", number
NameError: name 'real_number' is not defined"

guess_7_times()

if won == False:
     print('you lost....')
     print "The number was", number
else:
     pass

hehe, i kinda got carried away..

# import all of our needed random functions
import random

won = False

def guess_7_times():
     global won
     print('Welcome to the number guessing game!')
     # our random number
     real_number = random.randint(1,100)
     # how may guesses we have until we loop out of the function and exit
     guesses = 7

     # a loop; it will keep runing as long as guesses > 1
     while guesses > 0:
          guess = input('Guess a number between 1 and 100! ')
          # if our guess is too big
          if guess > real_number:
               print('Your guess is to big!\n')
          # if our number is less than real_number..
          elif guess < real_number:
               print ('Your number is too small!\n')
          # or, if our guess IS equal to real_number
          elif guess == real_number:
               print('Woot! You guessed the correct number!!')
               # print you got the correct number. if your wondering what
               # +str(real_number) means, all we are doing is converting our
               # number, real_number, into a string that python can print.
               # for some reason, python wont let you combine strings and numbers
               # when printing text, so we have to convert it into a string.
               print('The correct number was '+str(real_number))
               won = True
               # 'break' out of our loop
               break

          
          # minus guesses by 1 each loop
          # note: guesses -= 1 is the same as saying
          # guesses = guesses-1
          guesses -= 1
          
         
# let's call our function!
guess_7_times()

if won == False:
     print('you lost....')
else:
     pass

BTW http://en.wikibooks.org/wiki/Python_Programming/Functions is a great page for learning about functions. I would check it out!

thx for the help but what does this operation do?"global"?
and what if we make guess=7 and then while guess>0 does this mean it would keep the while loop going until the guess is less than 0?

thx for the help but what does this operation do?"global"?

Global should not be used at all,it`s no good at all.
Learn to code without it.

Here one way to do what you want.

import random 
  
def main(max_guess):     
    num = random.randrange(1, 100) 
    tries = 0 
    guess = "" 
    while guess != num: 
        guess = int(raw_input("Take a guess: ")) 
        if guess > num: 
            print "Too high." 
        elif guess < num: 
            print "Too low." 
        tries += 1 
        if tries >= max_guess: 
            print "Sorry, you took too many guesses. Game Over" 
            exit() 
    print "Congratulations! you did it in %d tries" % (tries)
    again = raw_input("To play again press Enter. Type anything else to quit.") 
    if again == "": 
        main(7) 
    else: 
        exit() 
#We pass in max gusses as an argument  
main(7)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.