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

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.

#! /usr/bin/python

def check_range(n, low, high):
    return (low <= n <= high)

# use an endless loop and a condition to break out of it
while True:
    s = raw_input("Enter a number between 1 and 10 (q to quit): ")
    if s == 'q':
        break
    n = int(s)
    # check range 1 to 10 inclusive
    if check_range(n, 1, 10):
        print "you entered", n
        print "the square is", n*n
    else:
        print "not in range, try again ..."

Nice clean code there Lardmeister. Did you switch from C# to Python?

That is clean code!

I guest i needed to add some comments.
But hey, thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.