Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

A small problem in my calc program

calculator

  • Please log in to reply
18 replies to this topic

#13 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2313 posts

Posted 04 February 2008 - 09:52 PM

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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#14 nolls

nolls

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 05 February 2008 - 12:09 PM

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.
  • 0

#15 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2313 posts

Posted 05 February 2008 - 11:54 PM

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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#16 nolls

nolls

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 26 February 2008 - 07:39 PM

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:
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"

  • 0

#17 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2313 posts

Posted 26 February 2008 - 09:52 PM

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:
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.
# 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!"

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#18 nolls

nolls

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 27 February 2008 - 07:03 PM

Ok thanks for the advice void :). Here is the new 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 :(.
  • 0

#19 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2313 posts

Posted 27 February 2008 - 09:58 PM

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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download