| | |
A little help tweaking the my code
Thread Solved |
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:
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...
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:
Python Syntax (Toggle Plain Text)
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.
python Syntax (Toggle Plain Text)
#! /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 ..."
I upped my sanitary measures, up yours!
![]() |
Similar Threads
- Help tweaking some java (JavaScript / DHTML / AJAX)
- Date fields imported from access file to excel spreadsheet is converting to number (Visual Basic 4 / 5 / 6)
- Linked Lists in C (C)
- Dani Webs Code? (JavaScript / DHTML / AJAX)
- Need help tweaking my program (Java)
Other Threads in the Python Forum
- Previous Thread: wxpython
- Next Thread: thread,time, sleep
| Thread Tools | Search this Thread |
accessdenied advanced aliased argv beginner bits calling casino change command convert count csv cturtle cursor def dictionary digital dynamic dynamically enter event examples external file float format frange function google gui hints homework i/o iframe import input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame multiple newb number numbers obexftp output parameters parsing path port prime programming projects py py2exe pygame pygtk pyopengl python random recursion remote return reverse scrolledtext session signal simple skinning sprite string strings syntax terminal text threading time tkinter tlapse tuple tutorial ubuntu unicode unit urllib urllib2 variable voip web-scrape whileloop wxpython






