Closed Thread
Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: A small problem in my calc program

  1. #11
    nolls is offline Newbie
    Join Date
    Jan 2008
    Posts
    18
    Rep Power
    0
    Thank you for all of the assistance void. Gladly appreciate it .

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #12
    nolls is offline Newbie
    Join Date
    Jan 2008
    Posts
    18
    Rep Power
    0
    Also, what exactly is Python used to create? I am having trouble coming up with some projects to create that will help me get a better understanding of python.

    I don't know if this is because I don't know enough yet, but I also don't know what python is mostly used for. Once I do know, it might be easier for me to create some projects/benchmarks for me to acheive. Any help greatly appreciated.

    Best regards

  4. #13
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    Python is used for a lot of different things. Everything from web-programming to GUI-programming. I think it's used most for small scripts to manage the system, though.

    I think you shall just think about what you would like to make, and than start making it. You may need a third-party library, but fortunately there's a lot of those around. Personally, I like CherryPy, Cheetah and wxPython.

  5. #14
    nolls is offline Newbie
    Join Date
    Jan 2008
    Posts
    18
    Rep Power
    0
    I am not really clear on what purpose GUI serves. Is it basically the overall appearance of a program?

    Now I am learning about functions and defining your own. Got past strings, tuples, lists, dictionaries, methods etc. So I am still a n00b to python and to programming in general. So with what I have learned so far is there much to create? I have been thinking of what else I could, but I am stumped.

  6. #15
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    GUI means Graphical User Interface, so it's everything you're looking at. Like now, when you read this post you'll probably be reading it from a browser; this browser have a graphical user interface - it's everything you're looking at. As default, Python ships with Tkinter, so you could try out some GUI-programming right away, without installing new libraries.

    From what you're saying you know, you should be able to make the famous guess-a-number-game. It requires you to have the basic understanding of modules and user I/O though.

  7. #16
    nolls is offline Newbie
    Join Date
    Jan 2008
    Posts
    18
    Rep Power
    0
    Ok, after coming back to learn Python I decided to try and write a guess-a-number game. Although, I don't know how to make the "number" be randomly assigned to a different number every guess. So I just designated the number 3 as the "number". How would I be able to make the number switch every time though?

    Here is the code:
    Code:
    print "Welcome to the Guess a Number game"
    print "Goal: To guess the number the computer is thinking of"
    number = 0
    while number != 6:
        print "1. Number 1"
        print "2. Number 2"
        print "3. Number 3"
        print "4. Number 4"
        print "5. Number 5"
        print "6. Exit"
        number = input("Which number would you like to guess? ")
        if number == 1:
            print "Nope! Try again"
        elif number == 2:
            print "Nope! Try again"
        elif number == 3:
            print "Yes, you guessed right!"
        elif number == 4:
            print "Nope! Try again"
        elif number == 5:
            print "Nope! Try again"

  8. #17
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    You can generate random numbers using the module random. There's lots of ways to use the module, so you may want to check out the documentation for all the information. This is an example on how it could be done:
    Code:
    import random
    
    # Generate a number between 1 and 10 (including 1 and 10)
    number = random.randint(1, 10)
    You should consider modifying your code a little too. You've hardcoded all the possibilities, which is inefficient. I would rather compare the input with the random number.
    Code:
    # Check if the input is valid (1-10)
    if input >= 1 and input <= 10:
        # Check if the input is greater than the random number
        if input > number:
            print "The random number is less than %d" % input
        # Check if the input is less than the random number
        elif input < number:
            print "The random number is greater than %d" % input
        # If non of the expressions above were true, it must be the right input.
        else:
            print "You guessed the right number, congratulations!"

  9. #18
    nolls is offline Newbie
    Join Date
    Jan 2008
    Posts
    18
    Rep Power
    0
    Ok thanks for the advice void . Here is the new code:

    Code:
    import random
    print "Welcome to the Guess a Number game"
    print "Goal: To guess the number the computer is thinking of"
    number = 0
    while number != 6:
        compnum = random.randint(1, 5)
        print "1. Number 1"
        print "2. Number 2"
        print "3. Number 3"
        print "4. Number 4"
        print "5. Number 5"
        print "6. Exit"
        number = input("Which number would you like to guess? ")
        if compnum >= 1 and compnum <= 5:
            if compnum > number:
                print "Too low, try again"
            elif compnum < number:
                print "Too high, try again"
            elif compnum == number:
                print "You guessed the right number, congrats!"
    I have some questions I would still like to ask though.

    Does randint mean random integers? Wouldn't randrange work also (while looking into module random that was what randrange was said to do, integers between parameters)?

    What did you mean by hardcode? And is this good enough or can it be modified further?

    Oh and is a floating number just a decimal/integer? Forgot the meaning of it .
    Last edited by nolls; 02-27-2008 at 07:12 PM.

  10. #19
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    You could probably use randrange too. I haven't played around with random for a long time, and the only function I remember was the one I used, randint.

    You "hardcoded" every possible input, you thought you could get from the user. That's no a good way to do it, if you later want to extend your program. Let's say you were just going to add more random number possibilities.

    Floating numbers are the numbers with fractional parts behind the decimal separator, like; 12.34, 1.34, 4353.2354, etc.

Closed Thread
Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Seeking Someone to Create a Small Program
    By GGEden in forum Request Services
    Replies: 3
    Last Post: 03-27-2011, 06:34 AM
  2. Small basic program (Geo)
    By Geo in forum Visual Basic Programming
    Replies: 1
    Last Post: 12-21-2010, 04:14 PM
  3. some errors in small program
    By onus in forum C and C++
    Replies: 2
    Last Post: 09-21-2010, 09:44 PM
  4. Small problem
    By Edvinas in forum PHP Development
    Replies: 2
    Last Post: 06-16-2010, 07:10 AM
  5. need help with a small program
    By scott0999 in forum General Programming
    Replies: 5
    Last Post: 02-28-2007, 10:19 PM

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