### Thread: This will be a joke to most of you

Page 1 of 2 12 Last
1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
8
Rep Power
0

#### This will be a joke to most of you

I know this is going to be like 2 + 2 for most of you, but I just started learning this not too long ago.

If I wanted to take a user valve, count up until that value is reached and print squares of all those numbers leading up to that value, how would I go about squaring them in order?

So if they enter 100, I want the square of 1-99, 1, 4, 9, 16, etc etc
2. Code:
```import sys
import pprint

def prompt(dataType, message = '? '):
sys.stdout.write(message)
for tries in range(3):
try:
result = dataType(a)
except:
pass
else:
return result
print('Enter '+str(dataType))
sys.stderr.write('UserError\n')
sys.exit(1)

def main():
value = prompt(int,'Enter a small integer: ')
try:
sign = value/abs(value)
except ZeroDivisionError:
sign = 1
print('values and squares')
pprint.pprint([(sign*i,i*i) for i in range(abs(value+sign))])

if '__main__' == __name__:
main()

import numpy

def main2():
a = numpy.arange(abs(prompt(int,'enter a number: ')))
pprint.pprint(numpy.concatenate((a,a**2)).reshape(2,len(a)).transpose())```
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
8
Rep Power
0
Thanks for the reply but I think that's just a little over my head.

I am learning while/if/loops/nested loops statements so it would need to be something like

user = float(input("Enter number: "))

x = 1
while x < user:
print(x)
x += 1

print("Done")

Something along those lines but outputting the numbers leading up to the user entered number squared. So if the user enters 10, the out would be 1, 4, 9, 16, 25, 36... to correspond with 1, 2, 3, 4, 5, 6, etc. as it counts to 10. I hope that made sense.
4. Before I go ahead with some suggestions, I need to mention something about this forum. The forum software does not automatically retain the indentation of code samples which are posted to it, with the result that code - especially Python code, where indent level is part of the block structure - becomes unreadable. To avoid this, you need to put your code samples in [code] tags, which preserve the formatting, or [highlight] tags, which do the same and add highlighting for keywords. Whenever you post code here, use the code button (the one that looks like a hash symbol) or the highlight button (the one that looks like a highlighter pen) to add the code snippet.

Also, I would like to know what version of Python you are using, and whether you are using IDLE or any other editor like that. If you aren't sure what version you are running, you can find it in the text which prints when you start the Python interpreter. For example, this is what Python 3.3 displays on my computer (note the use of code tags!):
Code:
`Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)] on Alpha-Complex, Standard`
If you're not sure how to read the version statement, post it here and we'll take a look at it for you.

The reason the version is important is because the language changed significantly going from version 2.x to version 3.x, and even some basic things like how you read in text have changed significantly.

OK, now that I've mentioned that, let's work on this a piece at a time. To start with, you use the function raw_input() (Python 2.x) or input() (Python 3.x - don't use input() in the earlier versions of the language, as it interprets the input in a potentially dangerous manner, which is why they changed it). Assuming that you are using Python 2.x, you would write:
Code:
`inString = raw_input("What number do you want to count to? ")`
This returns a string with the answer the user enters. However, because you want that as a number, you need to convert that to an integer before you can use it. The function to do this is called int(), and it converts a string to an integer (naturally enough). However, there's a catch: what if the user entered something other than a number? Well, in that case, int() sends out an kind of error message called an exception. Don't worry too much about what an exception is right now; just know that if an exception is raised, and you don't catch it, it will end the program with an error message and a traceback, like so:
Code:
```>>> int('a')
Traceback (innermost last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'a'```
(As an aside, if you are ever posting a question about an error that occurs in a program, and the program is giving a traceback like this, you'll want to include a copy of the traceback and the error message in your post. It may not make sense to you right now, but the traceback gives a lot of important information about why the program stopped.)

Anyway, getting back to what I'd been saying: to catch the exception, you need to put it in a try:.. except: block, like so:
Code:
```maxCount = 0
while maxCount == 0:
try:
maxCount = int(inString)
except:
print("You need to enter an integer value.")
maxCount = 0```
Note how I put the whole thing inside of a while: loop; this makes it so that the program will keep asking for the number until the user enters one correctly.

I'll let you play around with this for a bit before going on. BTW, have you read about for: loops yet? Because they're the best way to count something, as I'll show you shortly.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
5
Rep Power
0
Originally Posted by vogia
I am learning while/if/loops/nested loops statements
That totally makes sense, man, thanks for making sure. May I ask what you're using this application for? I'm learning as well (at about the same point, too!) and I'm derping around with ideas.
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
8
Rep Power
0
Thanks schol. The maxcount line looks right, I'll try when I get home. I'm using 3.2 and I do use idle to edit.
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
5
Rep Power
0

Originally Posted by vogia
I'm using 3.2 and I do use idle to edit.
I've been told never to use IDLE; I've been told to simply use a text editor for my Python programs (I use gedit) and then to do the following to allow them to be executed. After I've made my Python program executable, I just cd over to the directory my code is in and launch it.

Code:
```rhenzar@ubuntu:~\$ chmod +x my_python.py
rhenzar@ubuntu:~\$ python my_python.py```
It's real easy if you've never done that sort of thing before!

Also, I prefer gedit as my text editor because it highlights the Python syntax when you open .py files.

Hope I could help!
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
8
Rep Power
0
I think I get what you're saying but I am still a little lost.

If I do something like

Code:
```y = input("Number? ")
x = 1
while x < y:
Print(x)
x += 1
print("Done")```
and I input 10. It will output 2, 3, 4, 5, 6, 7, 8, 9, Done.

But i'm still not sure how to add 1 to the next number, then square it but then go back to the original number I added 1 to and not adding 1 to the number I just squared.
9. Change your
Code:
`       Print(x)`
to
Code:
`       print(x, x**2)`
10. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
6
Rep Power
0
Short And sweet:
Code:
`for i in map(lambda x: (x+1)**2,range(input("Count to?"))): print i`
11. TroyO: I'm not sure that that will help much, given that the OP hasn't learn about for: loops yet, never mind map(), or lambda. The goal here is to help the posters learn, not to confuse them further.

Also, your solution uses the Python 2.x form of input(), which as we all know is a Bad Thing that's been deprecated since Python 1.5 IIRC. More to the point, the OP already stated that they are using Python 3.2, in which case this won't even work as written.

OTOH, if one is going for a one-liner solution, then a list comprehension would probably be even simpler:
Code:
`[print(x**2, end=' ') for x in range(int(input("Count to? ")))]`
(This still leaves you hanging in the air if the user makes a mistake entering the value, but at least it doesn't lead to code injection risks.)

However, as I said, this doesn't help the OP one bit, since it is too unfamiliar to make heads or tails out of at this point.

Rhenzar: I have to admit that IDLE is far from an ideal working environment, but I wouldn't necessarily take that as a blanket condemnation of IDEs in general. Working style is as much a matter of personal preference as language preference is; and while I do agree that Vogia should probably at least learn to work from the shell and a basic editor, I'd also say that there are several excellent IDEs for Python available. I personally have been using Eric a lot lately, and find it quite useful; I also have Eclipse with PyDev and use that from time to time as well. I used to use Dr Python heavily, but got frustrated with it's limitations. For that matter, I'm an old hand at Emacs, which is still one of the best developer's editors around, if you can learn to use it. It's all in what feels most comfortable to you.
Last edited by Schol-R-LEA; November 2nd, 2012 at 01:02 PM.
12. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
8
Rep Power
0
Thanks for the help. The x, x**2 worked perfectly. I swear I tried that but obviously I was doing something wrong.
13. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
8
Rep Power
0
I didn't want to start a new thread for this but I have a question.

Since I am unable to send private messages, if someone who can send private messages, sends me one, I can reply to that person but that person only correct?
14. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
8
Rep Power
0
Well, let me just ask this.

I am trying to figure out how to make a program, using a while loop with a nested loop that will allow the user to input the number of students, and then the number of tests they took, and output the average for each student.

I am lost on how to get the program to loop back and ask for test scores until the number of tests has been reached, then dividing by the number of tests to get the average.

Right now I am just trying to get one student done, with no looping to other students.
15. Code:
```while unfinished(objects,changing,within,the,block):
changing = alter(objects,within,the,block)```
Page 1 of 2 12 Last