Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
How to make code run differently depending on the platform it is running on?
xarzu - Apr 05 2019 09:17 AM
How do I set a breakpoint in an attached process in visual studio
xarzu - Apr 04 2019 11:47 AM
Recent Blog Entries
Recent Status Updates
- Managed C++
- Visual Basic 4 / 5 / 6
- linked list
- hello world
A small problem in my calc programcalculator
Posted 04 February 2008 - 09:52 PM
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.
Posted 05 February 2008 - 12:09 PM
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.
Posted 05 February 2008 - 11:54 PM
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.
Posted 26 February 2008 - 07:39 PM
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"
Posted 26 February 2008 - 09:52 PM
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!"
Posted 27 February 2008 - 07:03 PM
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 .
Posted 27 February 2008 - 09:58 PM
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.
Also tagged with one or more of these keywords: calculator
Tutorial Forums →
Classes and Code Snippets →
Language Forums →
Language Forums →
C and C++ →
General Forums →
General Programming →
Language Forums →
Other Languages →