954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Python 2.6 User input

Hi I was reading the thread http://www.daniweb.com/forums/thread12326.html which is about getting user input from a user in python.

So I thought I would write something to tst it since I haven't used python in a while so I wrote a basic program to calculate netpay.

hours = raw_input("Enter Hours Worked: ")
gross = (hours * 12)
netpay = ( gross - ( gross * 0.15))
print "Your net wage is" $netpay


However I cannot get user input as the program doesn't wait for the user to input data just processes through, so how do you ge the program to stop for the user when getting input?

flebber
Light Poster
37 posts since Aug 2009
Reputation Points: 10
Solved Threads: 3
 

Perhaps it is just a mistake:
raw_input - takes a string, you need an integer. use just
hours = input("Here Hours...")
After print "Your net wage is", netpay (You are missing a comma after string)

radc
Newbie Poster
16 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

Thanks got it working also needed a pause to see output. is there a way to enforce output to have 2 decimal places?

import time
hours = input("Enter Hours Worked: ")
gross = (hours * 12)
netpay = ( gross - ( gross * 0.15))
print "Your net wage is $",netpay
time.sleep(2)
flebber
Light Poster
37 posts since Aug 2009
Reputation Points: 10
Solved Threads: 3
 

1. on line 4.... You are missing a coma.

print "Your net wage is", $netpay


2. This is python not php/perl

no need for

$netpay

just

netpay


Unless net pay is a template of which i dont think so. :)

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

what ide are you using?

it must stay up.
no need for time module.

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

Output with two numbers after comma? Try this:
f - for float point
d - for decimal
s - for string

print "netpay %-.2f" %(netpay)
radc
Newbie Poster
16 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

I am using notepad++ and without time module output flashes through without being read.

notepad++ isn't an ide I am saving output to desktop and running program from outside ide.

I cannot get decimal too work..still trying.

flebber
Light Poster
37 posts since Aug 2009
Reputation Points: 10
Solved Threads: 3
 

just try Idle or pydev/Aptana or netbeans.

They are all free and learning will be a breeze.

what OS are you on now?
:)
;)

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

Trying everything with decimal to get it too work but can't get it to work this is my last attempt following http://docs.python.org/library/decimal.html

import time
from decimal import *
Decimal(2)
hours = input("Enter Hours Worked: ")
gross = (hours * 12)
netpay = ( gross - ( gross * 0.15)) 
print "Your net wage is $", Decimal('netpay')
time.sleep(4)


Edit: Xp and 7. with a ubuntu box also.

And

import time
from decimal import *
hours = input("Enter Hours Worked: ")
gross = (hours * 12)
netpay().prec = 2
netpay = ( gross - ( gross * 0.15)) 
print "Your net wage is $", netpay
time.sleep(4)
flebber
Light Poster
37 posts since Aug 2009
Reputation Points: 10
Solved Threads: 3
 

Because we should be defining types before the math otherwise ( not likely in this example) get an incorrect result as an effect of rounding.

So how can I tell all my numbers in function to be 2 decimal places?

Edit maybe I can't define a varibale as 2 decimal places

Python figures out what type a variable is and keeps track of it internally.

from http://diveintopython.org/getting_to_know_python/declaring_functions.html

flebber
Light Poster
37 posts since Aug 2009
Reputation Points: 10
Solved Threads: 3
 

Just format your print like this

print( "netpay %-.2f" %(netpay))
richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

Like this no need to use time or decimal module.
Set upp notepad++ correct or use a good ide editor for python like pyscripter or pydev/SPE IDE
http://code.google.com/p/pyscripter/
http://pythonide.blogspot.com/
http://pydev.org/

hours = float(raw_input("Enter Hours Worked: "))
gross = hours * 12
netpay = gross - (gross * 0.15)
print "Your net wage is %.2f " % netpay


The code dos what you want it stop for user_input,and print out answer with 2 decimal places.
If it dos not,notepad++ is the problem. http://www.daniweb.com/forums/thread151270.html
http://cse.ucdavis.edu/~chaos/courses/nlp/Software/Windows/npp.html

snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: