import random

Number = random.randrange(100)+1
Tryz = 1


Attempt = int(raw_input("Please enter the number of attempt you want: "))
Guess = int(raw_input("Please guess the the number: "))

while Attempt != Tryz and Guess != Number:
    Tryz += 1
    if Guess < Number:
        Guess = int(raw_input("Try a higher number: "))
    else:
        Guess = int(raw_input("Try a lower number: "))
if Tryz == Attempt:
    print "You have ran out of tries the correct number is ", Number
else:
    print "You have guessed it in ", Tryz, "tries"
    print "The correct number was ", Number

Write a Python program that will ask the user how many tries s(he) needs for this game. The program must then randomly create an integer number between 0 and 100. The user must be prompted to guess this number. If the guessed number is lower or higher than the randomly created number, then the user must receive an appropriate message. The user must also receive and appropriate message to indicate when s(he) used up all their tries. The program must keep track of the number of tries and display it as soon as the user guessed it correctly. Below is an example of the output. (12)

>>> RESTART =______
How many tries do you think you need for this game?:    10
Enter any number between 0  and 100:    60
The number you entered istoohigh 
The number you entered istoohigh
The number you entered istoolow
The number you entered istoolow
You've guessed it in 6 tries'
The correct number was 16
> >> 
Could I have made my answer shorter since the question is 12 marks?

Edited 3 Years Ago by Dani: Formatting fixed

The else statement in the following code executes if the number is equal to or greater than since you don't test for an equal condition and will print "try a lower number" when equal. You should test for greater than only as the while loop catches the equal condition. Your Tryz variable is off by one (try the program with 2 attempts). Consider using a function that returns True or False and the number of tries. Also the Python style guide says that lower_case_with_underscores are to be used for variable names and Upper or CamelCase is for class names.

    if Guess < Number:
        Guess = int(raw_input("Try a higher number: "))
    else:
        Guess = int(raw_input("Try a lower number: "))

Edited 3 Years Ago by woooee

You could put the more repetitive tasks into a function. Something like this ...

import random

def get_guess(direction=""):
    return int(raw_input("Please guess a {} number: ".format(direction))) 

number = random.randrange(100)+1
print("Guess a number from 1 to 100 game ...\n")
tries = 1

attempts = int(raw_input("Please enter the number of attempts you want: "))
guess = get_guess()

while attempts != tries and guess != number:
    tries += 1
    if guess < number:
        guess = get_guess('higher')
    else:
        guess = get_guess('lower')

if tries == attempts:
    print "You have ran out of tries the correct number is ", number
else:
    print "You have guessed it in ", tries, "tries"
    print "The correct number was ", number

Edited 3 Years Ago by vegaseat

hi vegaseat , can you explain why you have direction="" in your function ? thanks

Argument direction picks up 'higher' or 'lower' and is an empty string by default for your starting call.

This question has already been answered. Start a new discussion instead.