字典程序的问题.... [英] Problem with a dictionary program....

查看:47
本文介绍了字典程序的问题....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。


我正在尝试编写一个小程序,让您输入一个数字作为

整数然后它会告诉你数字的文本表示。


如果你的输入是42,那就说四个。


我发现我有制作这样的字典:List = {1:" one",

2:" two"等等。


我必须使用raw_input方法来获取数字:


indput:raw_input("告诉我你希望转换为textuel的数字

代表")


我必须将输入转换为字符串

indput = str(indput)


这样我可以计算这个数字有多少位数,比如23有2位小数

和3000有4位小数。


在我得到字符串的长度之后,我将写一个循环,通过字典获得
的次数与字符串的长度一样多,并且

给了我相应的数字,数字21将通过

循环2次给我输出两个


你们其中一个人会如此善良并且告诉我如何计算输入的数量

数字我正在考虑像input.count [:]这样的东西但是那个dosnt

工作......


以及如何制作循环。


我试图理解字典但是有点卡住了...


感谢所有回复....

Hello.

I''m trying to write a small program that lets you put in a number as an
integer and then it tells you the textuel representation of the number.

Like if your input is 42, it will say four two.

I found out that I have to make a dictionary like this: List = { 1:"one",
2:"two" and so on )

and I have to use the raw_input method to get the number:

indput : raw_input(" Tell me the number you want to transform to textuel
representaion")

The I have to transform the input to a string
indput = str(indput)

so that I can count how many decimals the number has, like 23 has 2 decimals
and 3000 has 4 decimals.

After I have gotten the lenght of the string, I will write a loop, that goes
through the dictionary as many times as the lengt of the string, and the
gives me the corresponding numbers, the numner 21 would go 2 times through
the loop and give me the output two one

Will one of you be so kind and tell me how I count the lengt of the indput
number i was thinking on something like input.count[:] but that dosnt
work...

and how I make the loop.

Im trying to understand dictionaries but have gotten a bit stuck...

Thanks for all replies....

推荐答案

" Ling Lee" < JA ***** @ mail.trillegaarden.dk>在消息中写道

news:41 *********************** @ nntp05.dk.telia.net ...
"Ling Lee" <ja*****@mail.trillegaarden.dk> wrote in message
news:41***********************@nntp05.dk.telia.net ...

我必须使用raw_input方法来获取数字:

indput:raw_input("告诉我你要转换为textuel的数字 representaion")

我必须将输入转换为字符串
indput = str(indput)


实际上,返回的值是raw_input()*是*一个字符串,但你可能想要检查用户是否实际输入了一个整数,因为

反对输入,比如说,& ;走开你愚蠢的电脑。


试试:

indput = str(int(indput))
除了ValueError之外的


打印不,你需要给我一个整数。

你们其中一个人会这么善良并告诉我如何计算输入的长度
数字我正在考虑像input.count [:]这样的东西,但那个dosnt
工作......

and I have to use the raw_input method to get the number:

indput : raw_input(" Tell me the number you want to transform to textuel
representaion")

The I have to transform the input to a string
indput = str(indput)
Actually, the value returned by raw_input() *is* a string, but you might
want to check to see whether the user has actually typed in an integer as
opposed to typing, say, "Go away you stupid computer."

try:
indput = str(int(indput))
except ValueError:
print "No, you need to give me an integer."
Will one of you be so kind and tell me how I count the lengt of the indput
number i was thinking on something like input.count[:] but that dosnt
work...




你可以考虑使用内置函数len()。

-

我实际上没有读过我的hotmail帐户,但你可以用

如果你真的想和我联系,那就很兴奋。



You could consider using the built-in function len().
--
I don''t actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.


谢谢Russell Blau,它很聪明,因为它真的是一个整数

用户输入:)


我已经阅读了教程,但我仍然有点难以看到我如何制作

循环以及我如何计算数字的长度。当我使用len(输入)

时,我得到了回复:未确定对象的错误len()。想想这就是为什么len()

只适用于字典和列表,而不是字符串。


indput = raw_input("告诉我你想要的数字)转换为textuel

代表")

试试:

indput = str(int(indput))

除外ValueError:

print" No,你需要给我一个整数。


这样可以正常工作。


但是我如何计算数字的长度并制作循环:我的目标

是:


"我正在尝试编写一个小程序,让你输入一个数字作为

整数然后它会告诉你该数字的textuel表示。


如果您输入的是42,那就说四个。


我发现我必须制作这样的字典:List = {1:" one",

2:两个等等)


在我得到字符串的长度之后,我会写一个循环,通过字典就像
一样多次字符串的长度,

给我相应的数字,数字21将通过

循环2次,并给我输出两个。


很抱歉提出这个简单的问题,但在这个小小的

计划完成之后,我觉得事情真的会让我觉得很有意义。通过查看程序而不是阅读有关它的教程,我有更容易掌握pragramming的魔力

....


谢谢


" Russell Blau" < RU ****** @ hotmail.com>在消息中写道

news:2r ************* @ uni-berlin.de ...
Thanks Russell Blau, its smart to tjeck for it is really is an integer that
the user types in :)

I have read the tutorial, but still its a bit hard for me to see how I make
the loop and how I count the lengt of the number. When I use the len(input)
I get the reply: Error len() of unsized object. Think that is why len()
only works on dictionaries and lists, not strings.

indput = raw_input(" Tell me the number you want to transform to textuel
representaion")
try:
indput = str(int(indput))
except ValueError:
print "No, you need to give me an integer."

This works fine.

But how do i then count the length of the number and make the loop: My goal
was to:

" I''m trying to write a small program that lets you put in a number as an
integer and then it tells you the textuel representation of the number.

Like if your input is 42, it will say four two.

I found out that I have to make a dictionary like this: List = { 1:"one",
2:"two" and so on )

After I have gotten the lenght of the string, I will write a loop, that goes
through the dictionary as many times as the lengt of the string, and the
gives me the corresponding numbers, the numner 21 would go 2 times through
the loop and give me the output two one.

Sorry to ask this easy questions, but think that things really would make
sense for me after this little
program is done. I have a lot easier to grasp the magic of pragramming
through looking at programs than
to read tutorials about it....

Thanks

"Russell Blau" <ru******@hotmail.com> wrote in message
news:2r*************@uni-berlin.de...
" Ling Lee" < JA ***** @ mail.trillegaarden.dk>在消息中写道
新闻:41 *********************** @ nntp05.dk.telia.net ...
"Ling Lee" <ja*****@mail.trillegaarden.dk> wrote in message
news:41***********************@nntp05.dk.telia.net ...

我必须使用raw_input方法来获取数字:

indput:raw_input("告诉我你要转换为textuel的数字
代表)

我必须将输入转换为字符串
indput = str(indput)

and I have to use the raw_input method to get the number:

indput : raw_input(" Tell me the number you want to transform to textuel
representaion")

The I have to transform the input to a string
indput = str(indput)



实际上,raw_input()*返回的值是*一个字符串,但你可能想要检查用户是否实际输入了一个整数,因为
反对打字,比如说,走开你这个愚蠢的计算机。

尝试:
indput = str(int(indput))
除了ValueError:
print"不,你需要给我一个整数。



Actually, the value returned by raw_input() *is* a string, but you might
want to check to see whether the user has actually typed in an integer as
opposed to typing, say, "Go away you stupid computer."

try:
indput = str(int(indput))
except ValueError:
print "No, you need to give me an integer."

你们其中一个人会如此善良并告诉我如何计算
输入的数量
数字我正在思考像input.count [:]这样的东西
工作......
Will one of you be so kind and tell me how I count the lengt of the
indput
number i was thinking on something like input.count[:] but that dosnt
work...



您可以考虑使用内置函数len()。

-
我实际上并没有读过我的hotmail帐户,但你可以用
激活替换hotmail如果你真的想和我联系。



You could consider using the built-in function len().
--
I don''t actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.



Ling Lee写道:
Ling Lee wrote:
我得到之后这个字符串的长度,我会写一个循环,这个字符串经过字典很多次,而
给我相应的数字,数字21会去2次
通过循环给我输出两个。
After I have gotten the lenght of the string, I will write a loop, that
goes through the dictionary as many times as the lengt of the string, and
the gives me the corresponding numbers, the numner 21 would go 2 times
through the loop and give me the output two one.




没有必要计算长度。您可以迭代

a Python字符串(或其他对象)中的每个字符,而无需先计算

循环的大小,如下所示:


输出= []

for indput中的字符:

output.append(List [character])

print'', ''.join(输出)


正如Russel指出的那样,你必须像字符串一样迭代输入 -

不能转换它首先是一个整数,因为你不能迭代一个

整数'的数字,但是你可以迭代一个字符串的字符。


Jeffrey



There is no need to count the length. You can iterate over each character in
a Python string (or other object) without first calculating the size of the
loop, like so:

output = []
for character in indput:
output.append(List[character])
print '', ''.join(output)

As Russel pointed out, you''ll have to iterate over indput as as a string --
not convert it to an integer first, because you can''t iterate over an
integer''s digits, but you can iterate over a string''s characters.

Jeffrey


这篇关于字典程序的问题....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆