How would I change the output of my python program from being a number to being a string. In the simplest and best way
For example: instead of 1 it being "one"
This is probably a stupid question but I can't think of anything
6 3201
Hi
I think you'd have to do this the hard way. Although maybe there's a module or piece of code out there somewhere that's done it already. Probably a good idea to make a class which has the various conversions.
You'd need to make a couple of lists, and pull them out. E.g. units=['zero','one','two','three',...,'nine']. Then units[n] would give you the string.
Then you'd need to make a similar string for the teens=['eleven','twelve',...'nineteen']
A similar one for the tens=['','ten','twenty',...,'ninety'].
Then you'd have to split your number into bits (e.g. make it a string, slice it and convert the slices back to numbers), and use a serious number of if statements to put the text number together. You'll need to set an upper bound on this!!
bvdet 2,851
Expert Mod 2GB
Good advice Glenton. Let's say you put the upper limit at 9999. Without showing you a complete solution, you might set up your output like this: -
numDict1 = {0: '', 1:'one', 2:'two', 3:'three', 4:'four', 5:'five', \
-
6:'six', 7:'seven', 8:'eight', 9:'nine', 10:'ten', \
-
11:'eleven', 12:'twelve', 13:'thirteen', 14:'fourteen', \
-
15:'fifteen', 16:'sixteen', 17:'seventeen', 18:'eighteen', \
-
19:'nineteen', 20:'twenty', 30:'thirty', 40:'forty', \
-
50:'fifty', 60:'sixty', 70:'seventy', 80:'eighty', 90:'ninety'
-
}
-
thousands, rem = divmod(num, 1000)
-
hundreds, rem = divmod(rem, 100)
-
tens, ones = divmod(rem, 10)
Ok but lets say I want to make a simple calculator that adds two numbers and outputs the word. I'm not sure how to incorporate the above into the output.
bvdet 2,851
Expert Mod 2GB
I would like for you to show some effort to code this for yourself. Here's a hint: - numDict1 = {0: '', 1:'one', 2:'two', 3:'three', 4:'four', 5:'five', \
-
6:'six', 7:'seven', 8:'eight', 9:'nine', 10:'ten', \
-
11:'eleven', 12:'twelve', 13:'thirteen', 14:'fourteen', \
-
15:'fifteen', 16:'sixteen', 17:'seventeen', 18:'eighteen', \
-
19:'nineteen', 20:'twenty', 30:'thirty', 40:'forty', \
-
50:'fifty', 60:'sixty', 70:'seventy', 80:'eighty', 90:'ninety'
-
}
-
-
num = 9999
-
-
thousands, rem = divmod(num, 1000)
-
hundreds, rem = divmod(rem, 100)
-
tens, ones = divmod(rem, 10)
-
-
print numDict1[thousands], "thousand"
The output:
>>> nine thousand
Yeah sor I haven't review python quite enough. I got it to work but it won't go over 21. But thanks I think I could figure it out myself now. - while True:
-
import time
-
print "Testing word calculator v.1"
-
time.sleep(1)
-
S = input("Enter first number to be added: ")
-
S2 = input("Enter second number to be added: ")
-
num = S + S2
-
numDict1 = {0: '', 1:'one', 2:'two', 3:'three', 4:'four', 5:'five', \
-
6:'six', 7:'seven', 8:'eight', 9:'nine', 10:'ten', \
-
11:'eleven', 12:'twelve', 13:'thirteen', 14:'fourteen', \
-
15:'fifteen', 16:'sixteen', 17:'seventeen', 18:'eighteen', \
-
19:'nineteen', 20:'twenty', 30:'thirty', 40:'forty', \
-
50:'fifty', 60:'sixty', 70:'seventy', 80:'eighty', 90:'ninety'
-
}
-
thousands, rem = divmod(num, 1000)
-
hundreds, rem = divmod(rem, 100)
-
tens, ones = divmod(rem, 10)
-
-
print "Your number is " + numDict1[num]
But thanks for the advice
You're basically there!
You just need to build up your final number: -
output=""
-
if thousands>0:
-
output+=numDict1[thousands]+" thousand "
-
if hundreds>0:
-
output+=numDict1[hundreds]+" hundred "
-
if tens>1:
-
output+=numDict1[10*tens]+" "
-
if ones>0:
-
output+=numDict1[ones]
-
else:
-
output+=numDict1[10*tens+ones]
-
-
output=output.strip() #to get rid of trailing spaces that may be left
-
Note this gives the American way of saying numbers, which drops the "and" after hundred ("four hundred and seventeen" vs "four hundred seventeen"). If you want it the other way, you'll just have to fiddle a bit!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Gavin |
last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other
groups in a hope to get a response from anyone.
Can any one tell me how to make my VB program read the Bios serial number...
|
by: netclectic |
last post by:
Hi folks, i've searched and searched and can't find any example of
what i'm trying to do. Essentially (i think) i need to add a new
operator to Number.
I'm using eval to evaluate expressions...
|
by: Jason |
last post by:
Hi, below is example code which demonstrates a problem I have encountered.
When passing a number to a function I compare it with a string's size and
then take certain actions, unfortunately during...
|
by: B. Fletcher |
last post by:
Hi,
I'm having some trouble with javascript code of mine:
When the script runs, I vget an error in line 119: Number Expected.
I'm not sure as to why this is happening. Any advice would be...
|
by: Davey |
last post by:
How do I display an integer in binary format in C?
e.g. 4 displayed as "100"
|
by: Andrew Arace |
last post by:
I searched for something like this existing already, failing to find
it, I wrote it myself. If this already existed somewhere in the
framework I appologize for my ignorance.
This method will...
|
by: Jovo Mirkovic |
last post by:
Hi,
I have to make a program which will generate 100,000 different ID
numbers (for example 2345-9341-0903-3432-3432 ...) It must be really
different, I meen it can not be a similar (like...
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
|
by: VK |
last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/
b495b4898808fde0>
is more than one month old - this may pose problem for posting over
some news servers. This is why I'm...
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a
string to a number?...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |