Hi, all.
I am currently working on my first Python program but I am having a bit of trouble with it. The program asks the user to input a number and then it checks to see if that number is greater than, less than, or equal to seven, and prints the adequate message.
What I can't figure out is how to get Python to ask the user if they think that the number they inputed is greater than or less than seven,
short answer - using raw_input(), same way as you got the user to input the number in the first place.
btw - raw_input returns a string, so in your example you'd have a problem with the test
if num > 7 ... because you're comparing a string with an integer ...
you probably want something like
num = int(raw_input("Enter a number: "))
and we'll worry about error checks later, in case they type something other than a number.
and then have Python somehow use that to then calculate the 'if num > 7: print "True" etc, "etc, etc"' functon (ie. if the user inputs "Yes" or "y" when asked if they think that the number they inputed - lets say 8 - is greater than 7, Python will then say "True, 8 is greater than 7." The closest I have gotten is:
You might do something like ...
print ## blank line num = int(raw_input("Enter a number: "))
guess = raw_input("Do you think that's greater than 7 ?")
guess = guess.lower()[0]
if guess == "y": if num > 7: print "True,", num, "is greater than 7." elif num < 7: print "False,", num, "is not greater than 7." elif num == 7: print "Wait a second, these numbers are the same." else: if num < 7: print "True,", num, "is less than 7." elif num > 7: print "False,", num, "is not less than 7." elif num == 7: print "Wait a second, these numbers are the same."
There are all kinds of ways you can improve this, but that's a simple way to do it.
-- Alex Tweedly, python beginner.
--- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.493 / Virus Database: 292 - Release Date: 25/06/2003