Page 11 of 15 FirstFirst ... 910111213 ... LastLast
Results 101 to 110 of 144

Thread: [Beginner] Programming Challenge: 5

  1. #101
    Join Date
    Jul 2008
    Beans
    1,706

    Re: [Beginner] Programming Challenge: 5

    Quote Originally Posted by cmay View Post
    no prob.
    lazarus is the delfhi like ide where you greate gui by dragging and dropping objects on a form write a procedure and then design your projects just like delfhi works
    ya i use lazarus in windows because it compiles object and regular pascal...but in linux i would just use a CLI compiler

  2. #102
    Join Date
    Aug 2008
    Beans
    Hidden!

    Re: [Beginner] Programming Challenge: 5

    Quote Originally Posted by davidcaiazzo View Post

    Code:
    def game(correctnum,count):
    	print "Please guess a number between 1-100: "
    	guess=int(raw_input())
    	if guess==correctnum:
    		return
    	while guess!=correctnum:
    		count+=1
    		if guess<correctnum:
    			print "Sorry you guessed too low!\nTry again: "
    			guess=int(raw_input())
    		if guess>correctnum:
    			print "Sorry you guessed too high!\nTry again: "
    			guess=int(raw_input())
    	return count
    I wonder if you actually ran your program.

    I might be mistaken as I have yet to code any Python code. However, at a first glance I appear to find two problem areas:

    The if statement just in front of the while does not return a value if the condition is met. Also, it is perfectly redundant since the while statement would fall through in that case anyway.

    Also, you might want to check the placement of the statements which read the next guess within the while. I would think that your program outputs a wrong count if you enter first a low and then a high guess.

    The structure of your program is very clean and easy to follow. I would not have been able to spot those two issues at a glance if that was not the case. You'd get points on that count, anyway.

  3. #103
    Join Date
    Dec 2007
    Beans
    2

    Re: [Beginner] Programming Challenge: 5

    Aswell better late than never, here is my entry using C++, which i've just started.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void startguess(int num);
    int main()
    {
    	srand((unsigned)time(0));
    	startguess((rand()%100)+1);
    	return 0;
    }
    void startguess(int num)
    {
    	int guess=0, guesses=0;
    	while(guess != num && guesses++ != -1){	
    		cout << "Guess the number, 1-100: ";
    		cin >> guess;
    		if(guess > num) cout << "Not so high!\n";
    		else if(guess < num) cout << "That's too low!\n";
    	}
    	cout << "You're damn right. It took you " << guesses << " guesses.\n";
    }
    Last edited by cybermacro; September 30th, 2008 at 11:35 AM.

  4. #104
    Join Date
    Dec 2006
    Location
    Hogwarts, `UNKNOWN`
    Beans
    Hidden!
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: [Beginner] Programming Challenge: 5

    I suppose I am involving in necromancy now. But Here We Go then:

    Code:
    #!/usr/bin/python
    print"""#################
    # Guessing Game #
    #################""", '\n\n'
    
    import random
    
    numGuess = 0
    tries = 0
    
    RandomNum = random.choice(range(1,101))
    
    
    print "Random Number Generated\n\n"
    
    while True:
    	numGuess = input("Enter the number which you think the computer has selected: ")
    	tries+=1
    	
    	if numGuess == RandomNum:
    		print '\nCongratulations. You selected the right number'
    		break
    	elif numGuess > RandomNum:
    		print '\nYou guessed higher than the number actually is...'
    	else:
    		print '\nYou guessed lower than than the number actually is...'
    		
    	
    print "You guessed the number", RandomNum, " in ", tries, " attempts."
    I must mention I did not know how to randomize and I took the idea from cardboardtoast. I guess random.choice(range(1,101) will randomly give a number b/w 1 and 100 and its second argument is exclusive.

    BTW It's interesting that we don't need to randomize the timer. Cool.

  5. #105
    Join Date
    May 2008
    Location
    France
    Beans
    120
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: [Beginner] Programming Challenge: 5

    Quote Originally Posted by Anurag_panda View Post
    I must mention I did not know how to randomize and I took the idea from cardboardtoast. I guess random.choice(range(1,101)) will randomly give a number b/w 1 and 100 and its second argument is exclusive.
    python has random.randint(1,100).

    Here's the official module documentation index: http://docs.python.org/modindex.html
    À la chasse au boson intermédiaire

  6. #106
    Join Date
    Jul 2008
    Beans
    1,706

    Re: [Beginner] Programming Challenge: 5

    i love scheme

    PHP Code:
    (define (getmax)
      (
    begin
        
    (flush-output)
        (
    display "hello, pick a number that will be top of the guessing range\n")
        (
    flush-output)
        (
    newline)
        (
    read)))

    (
    define (check number)
      (
    define input 0)
      (
    display "what is your guess?")
      (
    newline)
      (
    flush-output)
      (
    setinput (read))
        (
    cond ((< input number) (display "too low!") (newline) (check number))
          ((= 
    input number) (display "you win!") (newline))
          ((> 
    input number) (display "too high!") (newline) (check number))))

    (
    define (game) (define input 0) (check (random (getmax))) (display "play again"))

    ;
    this is a comment this part simply calls the game method
    (begin (game)) 

  7. #107
    Join Date
    Aug 2006
    Location
    Connecticut
    Beans
    33
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: [Beginner] Programming Challenge: 5

    Using python3 .

    PHP Code:
    #!/usr/bin/python3

    import random



    target 
    random.randint(1,100)
    test = -1
    attempts 
    0



    def check_int
    (user_num):
        
    ''' Verify that the input is an integer'''
        
    try:
            
    user_num int(user_num)
            return 
    True
        except TypeError
    :
            print(
    ' Please input an integer.')
            return 
    False
        except ValueError
    :
            print(
    ' Please input an integer.')
            return 
    False
            
    def check_range
    (user_num):
        
    ''' Verify that the input is between 1 and 100'''
        
    good_guess True
               
        
    if user_num <= 0:
            print(
    ' Please input an integer between 1 and 100.')
            
    good_guess False
               
        
    if user_num 100:
            print(
    ' Please input an integer between 1 and 100.')
            
    good_guess False
        
        
    return good_guess

    def check_guess
    (user_num):
        
    '''Test the user guess and provide feed back'''
        
    if check_range(user_num):
            if 
    user_num target:
                print(
    'Guess Higher:')
                
            if 
    user_num target:
                print(
    'Guess Lower:')
                
            if 
    user_num == target:
                print(
    ' Congrats you guessed the correct number ' str(target) +'.')
                
                if 
    attempts 1:
                    print(
    ' It took you ' str(attempts) +' attempts.')
                else:
                    print( 
    'It took you one try!')
                
        return 
    user_num

    # The actual game takes place below.

    print(' In this game you must guess the randomly selected number between 1 and 100.')
    print(
    ' Only input integers between 1 and 100.\n The game will stop after you \
    have made a correct guess.\n \n Make a guess:'
    )

        
    while 
    test != target:
        
    guess input()
        
    attempts attempts 1
        
    if check_int(guess):
            
    test check_guess(int(guess)) 

  8. #108
    Join Date
    Jan 2009
    Location
    USA
    Beans
    20
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: [Beginner] Programming Challenge: 5

    Better late than never I suppose, did it in C.
    Use "gcc game.c -o game", then "./game" to run

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argcchar** agv)
    {
        
    time_t seconds;
        
    time(&seconds);
        
    srand((unsigned intseconds);
        
    int correctNum rand() % (100 1);
        
    int uinput;
        
    int tries;

    printf("\nYour objective is to guess a random number between 1 and 100.\n(our seed is %d by the way)\n\n"seconds);
        for(
    tries=1;tries <= 10;tries++)
        {
            
    printf("Enter a number: ");
            
    scanf("%d", &uinput);
            if (
    uinput correctNumprintf("too low, %d guesses left\n\n", (10 tries));
            if (
    uinput correctNumprintf("too high, %d guesses left\n\n", (10 tries));
            if (
    uinput == correctNum)
            {
                
    printf("Correct!  You used %d guesses\n"tries);
                return 
    0;
            }    
        }
        
    printf("You fail!  The number was %d!"correctNum);
        return 
    0;

    Last edited by module0000; January 19th, 2009 at 03:59 AM. Reason: turned on syntax highlighting

  9. #109
    Join Date
    Jul 2008
    Location
    Athens, Georgia
    Beans
    228

    Re: [Beginner] Programming Challenge: 5

    A bit of irony: it looks like LaRosa wants you to write code to make the USER do the binary search! <chuckle>

  10. #110
    Join Date
    Oct 2008
    Location
    $HOME
    Beans
    112
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: [Beginner] Programming Challenge: 5

    Comments/Advice Welcome

    PHP Code:
    #!/usr/bin/python
    import random
    guess
    ='0'
    tries=0
    num
    =random.randrange(1,100)
    #the whole guessing part
    while guess != 'quit' and int(guess) != num:
        
    guess=raw_input("Try to guess the number: ")
        if 
    int(guess) > num:
            print 
    "Too high"
        
    if int(guess) < num:
            print 
    "Too low"
        
    tries=tries+1
    #if they get it
    if guess != 'quit':
        print 
    "\nYou got it in %i tries\n" % (tries
    $(fortune)
    In a world without walls and fences, who needs windows and gates?

Page 11 of 15 FirstFirst ... 910111213 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •