| Quick question from a super noob. 
 
	 
	
		| Author | Message |   
		| SIXAXIS 
 
 
 
 
 | 
			
				|  Posted: Thu Feb 21, 2008 3:28 pm    Post subject: Quick question from a super noob. |  |   
				| 
 |  
				| Hi, 
 As you can see, I'm extremely new to Python, and although it's not the first language that I'm learning to program in, I don't know how to do anything. My question is: How do you make a variable a real number instead of an integer?
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sponsor Sponsor
 
  
   |  |   
		|  |   
		| Nick 
 
  
 
 
 | 
			
				|  Posted: Thu Feb 21, 2008 3:31 pm    Post subject: RE:Quick question from a super noob. |  |   
				| 
 |  
				| instead of assigning 10 to the variable foo let's call it, try assigning 10.0 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| McKenzie 
 
  
 
 
 | 
			
				|  Posted: Thu Feb 21, 2008 7:07 pm    Post subject: Re: Quick question from a super noob. |  |   
				| 
 |  
				| Right, unless your problem is something like: 
 	  | code: |  	  | 
n = input("Enter numerator: ")
 d = input("Enter denominator: ")
 print "that is", n / d
 | 
 in which case you need to use float if you want a decimal answer, like:
 
 	  | code: |  	  | 
print "that is", float(n) / d
 | 
 
 Keep in mind that Python is dynamically typed so:
 
 	  | code: |  	  | 
x = 12
 x = 4.5
 x = "Dog"
 | 
 works just fine.
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sniper4Life 
 
  
 
 
 | 
			
				|  Posted: Sun Apr 12, 2009 10:40 pm    Post subject: RE:Quick question from a super noob. |  |   
				| 
 |  
				| mckenzie...how would u set it so that the decimals can only go SO FAR...like you can only have 4 or 3 decimals...or only 1...?????? |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| [Gandalf] 
 
  
 
 
 | 
			
				|  Posted: Mon Apr 13, 2009 1:13 am    Post subject: RE:Quick question from a super noob. |  |   
				| 
 |  
				| Well, I'm not McKenzie, however... 
 	  | code: |  	  | >>> print "Amount: %.2f" % (3.141592653589)
Amount: 3.14
 | 
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sniper4Life 
 
  
 
 
 | 
			
				|  Posted: Mon Apr 13, 2009 9:55 am    Post subject: RE:Quick question from a super noob. |  |   
				| 
 |  
				| so....%2f should do it? 
 oh ya i tried your thing and it works
 ty
 but u cant use it if you do firstpx-secondpx as what your gonna round
 
 
 	  | code: |  	  | 
firstpx=input ("First point's x coordinate: ")
 firstpy=input ("First point's y coordinate: ")
 
 print " "
 secondpx=input ("Second point's x coordinate: ")
 secondpy=input ("Second point's y coordinate: ")
 print " "
 
 print "First point:  ","(","%.2f" % firstpx,",","%.2f" % firstpy,")"
 print "Second point: ","(","%.2f" % secondpx,",","%.2f" % secondpy,")"
 print " "
 
 
 
 
 print "Distance: ""(",firstpx-secondpx,",",firstpy-secondpy,")"
 
 
 | 
 
 k ive been stuck on this code like forever cus i dont know how to round it....at the last line when you print distance...how can u make it so you can round it?
 since its printing an operation...i dont kno...how to round it...O.O
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sniper4Life 
 
  
 
 
 | 
			
				|  Posted: Mon Apr 13, 2009 10:53 am    Post subject: RE:Quick question from a super noob. |  |   
				| 
 |  
				| nvr mind i just figured it out its very simple ^^ i nvr learned bout floats in python so it was a bit confusing for me...
 
 	  | code: |  	  | 
x=float(firstpx-secondpx)
 y=float(firstpy-secondpy)
 print "Distance: ""(","%.2f" % x,",","%.2f" % x,")"
 
 
 | 
 
 ya that solves my problem ^^
 (ps-awesome im not  a noob programmer any more
  ) |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Zeroth 
 
 
 
 
 | 
			
				|  Posted: Mon Apr 13, 2009 11:06 am    Post subject: Re: Quick question from a super noob. |  |   
				| 
 |  
				| Be very, very, very careful with using input. Its basically evaluating all input typed by the user. So one could feasibly use straight python code, write a function, or manipulate the program state via input. 
 Your usage of input means that the user could input either an int or a float. You need to use float(firstpx) to force them to floats.
 
 Second of all, its much nicer to use:
 
 	  | code: |  	  | 
print "First point: (%.2f,%.2f)" % (float(firstpx), float(firstpy))
 
 | 
 
 The use of the brackets around the two arguments creates a tuple, which python automatically unpacks to the positional arguments in the string.
 
 You can also do some really cool stuff like this:
 
 	  | code: |  	  | 
>>> d = {'car': 'haha', 'cdr': '!lisp'}
 >>> print "%(car)s" % d
 haha
 >>>
 
 | 
 
 What happened there is that the key is inside the ()'s, and the format is right after, the s. By passing a dict, you can fill in the string as necessary, so long as empty keys still have a value(ideally, just a blank string "").  Of course, any class that behaves like a dict will work here too
  . |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sponsor Sponsor
 
  
   |  |   
		|  |   
		| wtd 
 
 
 
 
 | 
			
				|  Posted: Mon Apr 13, 2009 11:10 am    Post subject: Re: RE:Quick question from a super noob. |  |   
				| 
 |  
				| Sniper4Life @ Mon Apr 13, 2009 11:40 am wrote: mckenzie...how would u set it so that the decimals can only go SO FAR...like you can only have 4 or 3 decimals...or only 1...?????? 
 Keep in mind that you can't control the accuracy of the information stored as a floating point number.  You can only control the accuracy of the output.
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sniper4Life 
 
  
 
 
 | 
			
				|  Posted: Mon Apr 13, 2009 11:27 am    Post subject: RE:Quick question from a super noob. |  |   
				| 
 |  
				| ya wtd thats what i meant for question introEx5 on mckenzie's exercises i need to control the output...
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		|  |  
 |