Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [SOLVED]Python input question
New Posts  All Forums:Forum Nav:

[SOLVED]Python input question

post #1 of 5
Thread Starter 
Hello,

I was wondering how do I have Python display a 0 after a period. An example is this

Code:
userNum = input("Please enter a number.")
Lets say the user enters 0.70.
print str(userNum)
Python will then output 0.7.
I am wondering how do I have Python actually output 0.70.
Edited by blue_k - 5/29/11 at 4:20am
Black_Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.5GHz Asus P8P67 PRO EVGA 012-P3-1470-AR GeForce GTX 470 8GB DDR3-1600 G.Skill Ripjaws X 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 & 640GB Western Digital Black ASUS DRW-24B1ST Windows 7 Professional x64 & Slackware64 13.37 CHIMEI 22" 1680x1050 
PowerCase
Corsair 750W CM HAF 922 
  hide details  
Reply
Black_Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.5GHz Asus P8P67 PRO EVGA 012-P3-1470-AR GeForce GTX 470 8GB DDR3-1600 G.Skill Ripjaws X 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 & 640GB Western Digital Black ASUS DRW-24B1ST Windows 7 Professional x64 & Slackware64 13.37 CHIMEI 22" 1680x1050 
PowerCase
Corsair 750W CM HAF 922 
  hide details  
Reply
post #2 of 5
It's been ages since I did Python, but from memory I think if you want X decimal places to show you type this:

[in your example for 2 decimal places]

print str("%.2f" % userNum)

Give it a go, i'm 50% sure it's right
post #3 of 5
Thread Starter 
That worked perfectly. Thank you so much hirolla888!
Black_Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.5GHz Asus P8P67 PRO EVGA 012-P3-1470-AR GeForce GTX 470 8GB DDR3-1600 G.Skill Ripjaws X 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 & 640GB Western Digital Black ASUS DRW-24B1ST Windows 7 Professional x64 & Slackware64 13.37 CHIMEI 22" 1680x1050 
PowerCase
Corsair 750W CM HAF 922 
  hide details  
Reply
Black_Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.5GHz Asus P8P67 PRO EVGA 012-P3-1470-AR GeForce GTX 470 8GB DDR3-1600 G.Skill Ripjaws X 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 & 640GB Western Digital Black ASUS DRW-24B1ST Windows 7 Professional x64 & Slackware64 13.37 CHIMEI 22" 1680x1050 
PowerCase
Corsair 750W CM HAF 922 
  hide details  
Reply
post #4 of 5
Quote:
Originally Posted by hirolla888 View Post
It's been ages since I did Python, but from memory I think if you want X decimal places to show you type this:

[in your example for 2 decimal places]

print str("%.2f" % userNum)

Give it a go, i'm 50% sure it's right
This would work in this particular case when the actual input number is also to 2 decimal places. However, if you do not know how many decimal places are in the input, you cannot get the output to the correct number of decimals.

For example:
If the user inputs 0.7000
The code you have given above will give an output of 0.70
If the user inputs 0.7
The code will give an output of 0.70 again

To the OP, I'm sorry I can't really help since I myself am not that much of an expert at Python. I'm still learning.

But if I remember anything that can work, I'll be sure to post here.
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #5 of 5
Thread Starter 
Quote:
Originally Posted by {Unregistered} View Post
This would work in this particular case when the actual input number is also to 2 decimal places. However, if you do not know how many decimal places are in the input, you cannot get the output to the correct number of decimals.

For example:
If the user inputs 0.7000
The code you have given above will give an output of 0.70
If the user inputs 0.7
The code will give an output of 0.70 again

To the OP, I'm sorry I can't really help since I myself am not that much of an expert at Python. I'm still learning.

But if I remember anything that can work, I'll be sure to post here.
Ok, thank you for the tip. In my case though, I know the user will not put more then 2 digits after the decimal, because my program deals with money.
Black_Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.5GHz Asus P8P67 PRO EVGA 012-P3-1470-AR GeForce GTX 470 8GB DDR3-1600 G.Skill Ripjaws X 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 & 640GB Western Digital Black ASUS DRW-24B1ST Windows 7 Professional x64 & Slackware64 13.37 CHIMEI 22" 1680x1050 
PowerCase
Corsair 750W CM HAF 922 
  hide details  
Reply
Black_Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.5GHz Asus P8P67 PRO EVGA 012-P3-1470-AR GeForce GTX 470 8GB DDR3-1600 G.Skill Ripjaws X 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 & 640GB Western Digital Black ASUS DRW-24B1ST Windows 7 Professional x64 & Slackware64 13.37 CHIMEI 22" 1680x1050 
PowerCase
Corsair 750W CM HAF 922 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [SOLVED]Python input question