Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: Python programing help

  1. #1
    Join Date
    Aug 2010
    Beans
    4

    Exclamation Python programing help

    i made a program in python, its a simple game it askes you for a number and if you guess it right you can play again, but its the same number. how can i put it that each time i guess the number right,to change the number itself,random each time you get it right.
    Attached Files Attached Files

  2. #2

    Re: Python programing help

    Hello and welcome to the Ubuntu Forums .

    You are nearly done

    Here is a very simple example for you to adapt. It will generate a random number between 0 and 100.

    Code:
    import random
    randomnumber = random.randint (0,100)
    print randomnumber
    Hope this helps you

    -Silver Fox

  3. #3
    Join Date
    Aug 2010
    Beans
    4

    Re: Python programing help

    thank you very much

  4. #4
    Join Date
    Aug 2010
    Beans
    4

    Re: Python programing help

    yes but even if i make the change it will ask you for a number between 0-100 and i type in 20 it says lower i type in 1 it says lower i type in 0 it still says lower????

  5. #5
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Python programing help

    I don't get it. The program returns "Its smaller" each time. i tried putting 0 and its the same thing. Something else is not good in there. Here is the code again.

    Code:
    import random 
    a = random.randint(0,100) 
     
    def guess(): 
     while 1: 
        number = raw_input ('Enter a number from 0 to 100: ') 
         
        if number == a:  
            print ("Congratulations You got it right!") 
            break 
            
        if number < a : print 'The number is higher' 
        if number > a : print ' The number is smaller' 
     
     
    guess()         
    while 2:     
         
       z= raw_input('Do you want to play again?y/n: ')      
       if z == 'y' : guess() 
       if z == 'n': break 
        
    raw_input('Press <enter> to quit>')

  6. #6
    Join Date
    Mar 2009
    Location
    Buenos Aires, AR
    Beans
    2,325
    Distro
    Ubuntu

    Smile Re: Python programing help

    Welcome, Fox is correct.
    Dont forget the:

    Code:
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    At the beginning of the file.

    #!/usr/bin/env python--->make it run by Python interpreter
    # -*- coding: utf-8 -*---->make it UTF-8 só yóú cán úsé Ⓢ!ⓜB0Ls™ Ññ ಠ_ಠ

  7. #7
    Join Date
    Aug 2010
    Beans
    4

    Re: Python programing help

    it still says its lower?

  8. #8
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Python programing help

    Its still returning "its smaller'. Here is the exact output!

    Code:
    Enter a number from 0 to 100: 0
     The number is smaller
    Enter a number from 0 to 100: 0
     The number is smaller
    Enter a number from 0 to 100: 50
     The number is smaller
    Enter a number from 0 to 100: 100
     The number is smaller
    Enter a number from 0 to 100:

  9. #9

    Re: Python programing help

    Hello again,

    Consider data types.

    Hope this helps you all.

    -Silver Fox

  10. #10
    Join Date
    Jun 2010
    Location
    /home/TX
    Beans
    225
    Distro
    Ubuntu 12.04 Precise Pangolin

    Post Re: Python programing help

    try displaying the number ( in this case "a" ) before each repetition/ guess. It'll help you debug the problem and find where the problem is (number input, random #s, if logic (probably not....)) even though it will defeat the point of the game.
    "The secret to creativity is knowing how to hide your sources." -Albert Einstein

    Ubuntu User #32977; Linux User #528876

Page 1 of 3 123 LastLast

Tags for this Thread

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
  •