A little help tweaking the my code

Thread Solved

Join Date: Jan 2008
Posts: 14
Reputation: Impact4ever is an unknown quantity at this point 
Solved Threads: 0
Impact4ever's Avatar
Impact4ever Impact4ever is offline Offline
Newbie Poster

A little help tweaking the my code

 
0
  #1
Feb 25th, 2008
Hi guys,

I have a friend at school and she's taken Unix 2. She gave me a copy of her work assignment so that I could improve my scripting skills (Still a begginner at python and programming).

This is the assignment:

Write a script that will prompt the user to input a number between 1 and 10.

You need to output:

1. The number they entered, with an explanation of what the number is.
2. The square of the inputted number.

Conditions:

1. You must check that the number is between 1 and 10, if not, clear the screen ask the user agian.

2. You must have an exit for the user, other than entering a number.

3. Use at least one sub function.


Ok so that it!

And this is what I made so far:

  1. 1 #! /usr/bin/python
  2. 2 #
  3. 3 # 1 and 10
  4. 4
  5. 5 import os
  6. 6 import sys
  7. 7
  8. 8 number = ""
  9. 9 while number > 0:
  10. 10 number = int(raw_input("\n\nChoose a number between 1 and 10: "))
  11. 11 print "This is the number you choosed", number
  12. 12 print " Square root of the number is",
  13. 13 print number * number
  14. 14
  15. 15 if number == 0:
  16. 16 print "\nThis needs to be a bigger number."
  17. 17 elif number > 10:
  18. 18 os.system("clear")
  19. 19 print "Opps!"
  20. 20
  21. 21 raw_input("\n\nTo exit program press the enter key. ")

The problem with the code is that it loops the way I wanted; but it won't exit out of the loop.
I know its something simple, but i'm not seeing is yet...
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,521
Reputation: Lardmeister is an unknown quantity at this point 
Solved Threads: 22
Lardmeister's Avatar
Lardmeister Lardmeister is offline Offline
Posting Virtuoso

Re: A little help tweaking the my code

 
0
  #2
Feb 25th, 2008
This is something similar to what you coded. I didn't use the 'clear the screen' thingy because that would make it OS specific, and our instructor says that it is better to leave a record on the screen anyway.
  1. #! /usr/bin/python
  2.  
  3. def check_range(n, low, high):
  4. return (low <= n <= high)
  5.  
  6. # use an endless loop and a condition to break out of it
  7. while True:
  8. s = raw_input("Enter a number between 1 and 10 (q to quit): ")
  9. if s == 'q':
  10. break
  11. n = int(s)
  12. # check range 1 to 10 inclusive
  13. if check_range(n, 1, 10):
  14. print "you entered", n
  15. print "the square is", n*n
  16. else:
  17. print "not in range, try again ..."
I upped my sanitary measures, up yours!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,536
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 170
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: A little help tweaking the my code

 
0
  #3
Feb 25th, 2008
Nice clean code there Lardmeister. Did you switch from C# to Python?
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 14
Reputation: Impact4ever is an unknown quantity at this point 
Solved Threads: 0
Impact4ever's Avatar
Impact4ever Impact4ever is offline Offline
Newbie Poster

Re: A little help tweaking the my code

 
0
  #4
Feb 25th, 2008
That is clean code!

I guest i needed to add some comments.
But hey, thanks for the help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC