gpt4 book ai didi

python - 我需要帮助将 Python 3.4.3 中的整数更改为单词

转载 作者:行者123 更新时间:2023-11-30 22:56:56 35 4
gpt4 key购买 nike

示例:1 = 一。

我所需要做的就是将计数增加到 5,但我的编码能力很差,而且很挣扎。代码要求是从输入的2-5之间的数字开始计数,向上数到5,但最后一个要求是输出必须是WORDED数字。

num = int(input('Enter a number (2-5): '))
count = 2
while count <= num:
if num > 5:
print('invalid.')
num = int(input('Enter a number (2-5): '))
print(count)
count = count + 1

我想要的打印是:

Enter a number (2-5): 
INPUT '5'
Two
Three
Four
Five

Enter a number (2-5):
INPUT '3'
Two
Three

目前我得到:

Enter a number (2-5): 
INPUT '5'
2
3
4
5

最佳答案

Python 并不能真正识别与数字相关的“单词”。代表数字的单词是一件非常人性化的事情。

您需要的是一个字典,其中数字的字符串版本是键,值是相应的单词。

例如:

words_num_dict = {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}
num = int(input('Enter a number (2-5): '))
count = 2
while count <= num:
if num > 5:
print('invalid.')
num = int(input('Enter a number (2-5): '))
print(words_num_dict[count])
count = count + 1

有关使用词典的更多信息 here

关于python - 我需要帮助将 Python 3.4.3 中的整数更改为单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36879060/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com